[英]python re backreference repeated elements
Let's say I have a string like this... 假设我有一个像这样的字符串......
myStr = 'START1(stuff); II(morestuff); 8(lessstuff)'
...and I want to extract the string immediately before the parentheses, as well as the string within the parentheses: 1
, stuff
, II
, morestuff
, 8
, lessstuff
. ... ...我想在括号前面提取字符串,以及括号内的字符串:
1
, stuff
, II
, morestuff
, 8
, lessstuff
。 I can achieve this using split(';')
, etc., but I want to see if I can do it in one fell swoop with re.search()
. 我可以使用
split(';')
等来实现这一点,但我想看看我是否可以用re.search()
一举完成它。 I have tried... 我努力了...
test = re.search( r'START(?:([I0-9]+)\(([^)]+?)\)(?:; )?)*', myStr ).groups()
...or in a more readable format... ...或者以更易读的格式......
test = re.search( r'''
START # This part begins each string
(?: # non-capturing group
([I0-9]+) # capture label before parentheses
\(
([^)]+?) # any characters between the parentheses
\)
(?:; )? # semicolon + space delimiter
)*
''', myStr, re.VERBOSE ).groups()
...but I only get the last hit: ('8', 'lessstuff')
. ......但我只获得最后一击:(
('8', 'lessstuff')
。 Is there a way to backreference multiple hits of the same part of the expression? 有没有办法反向引用表达式相同部分的多个命中?
You can use this regex in findall
to capture your text: 您可以在
findall
使用此正则表达式来捕获您的文本:
>>> myStr = 'START1(stuff); II(morestuff); 8(lessstuff)'
>>> print re.findall(r'(?:START)?(\w+)\(([^)]*)\)', myStr)
[('1', 'stuff'), ('II', 'morestuff'), ('8', 'lessstuff')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.