not returning the whole pattern in regex in python

后端 未结 2 1809
伪装坚强ぢ
伪装坚强ぢ 2021-01-22 06:11

I have the following code:

haystack = \"aaa months(3) bbb\"
needle = re.compile(r\'(months|days)\\([\\d]*\\)\')
instances = list(set(needle.findall(haystack)))
p         


        
2条回答
  •  再見小時候
    2021-01-22 06:49

    Parens are not just for grouping, but also for forming capture groups. What you want is re.compile(r'(?:months|days)\(\d+\)'). That uses a non-capturing group for the or condition, and will not get you a bunch of subgroup matches you don't appear to want when using findall.

提交回复
热议问题