Regex help on non-capturing groups

前端 未结 2 730
清歌不尽
清歌不尽 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: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

提交回复
热议问题