获取正则表达式以匹配组任意次数

时间:2014-03-03 00:42:42

标签: python regex

给出字符串:

Created by [[User:One|One]] ([[User talk:One|talk]]), [[User:Two|Two]] ([[User talk:Two|talk]]). Nominated by [[User:Three|Three]] ([[User talk:Three|talk]]) at 23:47, 25 January 2014 (UTC)

我怎样才能获得像(\[\[User:(.*?)\|.*?\]\]\s?)这样的正则表达式(与[[User:One|One]]匹配)以递归方式匹配它直到它到达. 

提前谢谢你。 ;)

1 个答案:

答案 0 :(得分:3)

您可以使用'findall'为您提供包含字符串中所有匹配项的列表:

import re

str = "Created by [[User:One|One]] ([[User talk:One|talk]]), [[User:Two|Two]] ([[User talk:Two|talk]]). Nominated by [[User:Three|Three]] ([[User talk:Three|talk]]) at 23:47, 25 January 2014 (UTC), How could I get a regex like (\[\[User:(.*?)\|.*?\]\]\s?)"

matcher = re.compile("(\[\[User:(.*?)\|.*?\]\]\s?)")
print matcher.findall(str)

请参阅http://docs.python.org/2/library/re.html#re.findall

如果你想总是在".&nbsp"完成,你可以拆分字符串,只把第一个元素作为:

untilnbsp = str.split(".&nbsp")[0]

这将只为您提供字符串"Created by [[User:One|One]] ([[User talk:One|talk]]), [[User:Two|Two]] ([[User talk:Two|talk]])",现在您可以使用findall