Regex help on non-capturing groups

前端 未结 2 731
清歌不尽
清歌不尽 2021-01-24 10:19

Must be a duplication but I can\'t seem to find it...

I am using a group to match a repeating sub-string. However, I do not want the group to be captured. This seems to

相关标签:
2条回答
  • 2021-01-24 11:01

    The reason this won't work is that when you write \1 you basically say "the content of the first group", which is of course undefined if the group is non-capturing.

    0 讨论(0)
  • 2021-01-24 11:13

    As re.findall will always fetch a list of tuples once you define several capturing groups in the pattern, you can't use "regex-only" approach here.

    Use re.finditer to get all match data objects and get Group 2 contents from each match only:

    print([x.group(2) for x in re.finditer(r'([A-Z]+)\1{2}(.)', s)])
    

    See the Python demo

    0 讨论(0)
提交回复
热议问题