python重新引用重复元素

时间:2016-05-06 17:21:27

标签: python regex backreference

我们说我有这样的字符串......

myStr = 'START1(stuff); II(morestuff); 8(lessstuff)'

...我想在括号前面提取字符串,以及在括号中提取字符串:1stuffIImorestuff8lessstuff。我可以使用split(';')等来实现这一目标,但我想知道我是否可以一次性使用re.search()来实现。我试过......

test = re.search( r'START(?:([I0-9]+)\(([^)]+?)\)(?:; )?)*', myStr ).groups()

......或者以更易读的格式......

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()

...但我只获得 last 点击:('8', 'lessstuff')。有没有办法反向引用表达式相同部分的多个命中?

1 个答案:

答案 0 :(得分:3)

您可以在findall中使用此正则表达式来捕获文本:

>>> myStr = 'START1(stuff); II(morestuff); 8(lessstuff)'
>>> print re.findall(r'(?:START)?(\w+)\(([^)]*)\)', myStr)
[('1', 'stuff'), ('II', 'morestuff'), ('8', 'lessstuff')]

RegEx Demo