简体   繁体   English

如何使用正则表达式提取同一模式的多个匹配项?

[英]How can I extract more than 1 match of the same pattern with regular expression?

For example, if I have 2 strings 例如,如果我有2个字符串

[Mandzukic 18' (o.g.) Griezmann 38' (pen.) Pogba 59' Mbappe 65'; Perisic 28' Mandzukic 69']

and

[Samuel Umtiti 51']

How can I write a single regex that can extract [Mandzukic, 18, Griezmann, 38, Pogba, 59, Mbappe, 65, Perisic, 28, Mandzukic, 69] from the first string and [Samuel Umtiti, 51] from the second string? 如何编写可以从第一个字符串中提取[Mandzukic,18,Griezmann,38,Pogba,59,Mbappe,65,Perisic,28,Mandzukic,69]并从第二个字符串中提取[Samuel Umtiti,51]的单个正则表达式?

This is what I have so far: (\\w\\s*\\w+)\\s(\\d+) but I don't know how to get it to extract more than 1 instance of this pattern 到目前为止,这是我所拥有的:(\\ w \\ s * \\ w +)\\ s(\\ d +),但我不知道如何获取它来提取此模式的多个实例

May be you this help, 可能是您的帮助,

    import re
    st = "Mandzukic 18' (o.g.) Griezmann 38' (pen.) Pogba 59' Mbappe 65'; Perisic 28' Mandzukic 69'"
    re.findall(r'(\w\s*\w+)\s(\d+)', st)
    # Output as: [('Mandzukic', '18'),
                  ('Griezmann', '38'),
                  ('Pogba', '59'),
                  ('Mbappe', '65'),
                  ('Perisic', '28'),
                  ('Mandzukic', '69')]

You can use re.findall to find all the matches of a pattern. 您可以使用re.findall查找模式的所有匹配项。 As for optionally matching a second word in your second sample input, you can put the word in a group with a ? 至于(可选)在第二个示例输入中匹配第二个单词,您可以将该单词放入带有?的组中? to make it optional: 使它可选:

import re
for s in "Mandzukic 18' (o.g.) Griezmann 38' (pen.) Pogba 59' Mbappe 65'; Perisic 28' Mandzukic 69", "Samuel Umtiti 51'":
    print(re.findall(r'(?:\w+\s+)?\w+\s+\d+', s))

This outputs: 输出:

['Mandzukic 18', 'Griezmann 38', 'Pogba 59', 'Mbappe 65', 'Perisic 28', 'Mandzukic 69']
['Samuel Umtiti 51']
s1="[Mandzukic 18' (o.g.) Griezmann 38' (pen.) Pogba 59' Mbappe 65'; Perisic
 28' Mandzukic 69']"
import re
re.findall('(\w\s*\w+)\s(\d+)', s1)
[('Mandzukic', '18'), ('Griezmann', '38'), ('Pogba', '59'), ('Mbappe', '65'), ('
Perisic', '28'), ('Mandzukic', '69')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 正则表达式:多行模式匹配两个以上的子字符串 - Python Regular Expression: Multiline pattern match with more than two substrings 如何提取 PySpark dataframe 中的正则表达式模式的所有实例? - How can I extract all the instances of a regular expression pattern in PySpark dataframe? 如何匹配此网址的正则表达式? - How can I match regular expression for this url? 如何修改此正则表达式以使用此模式提取字符串? - How to modify this regular expression to extract strings with this pattern? Readline 正则表达式匹配模式提取数字和时间 - Readline regular expression match pattern extract numbers and time Python如何匹配正则表达式模式 - Python how to match regular expression pattern Python正则表达式:是否存在用于搜索多个模式出现的符号? - Python Regular expression: is there a symbol to search for more than one occurence of a pattern? 正则表达式在双引号之间提取不超过十个单词 - Regular expression to extract not more than ten words between double quotes 如何在正则表达式中匹配单个和双重html属性? - How can I match single and double html attributes in regular expression? 如何建立正则表达式以匹配单个单词? - How can I build a regular expression to match a single word?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM