[英]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.