简体   繁体   中英

python using regex with groups, how do you get all the matches?

I'm using python re to match groups. When I use that with a + I'm seem to "lose" the first matches. Only the last one is seen by result.group(1). I do see them in result.group(0), but that doesn't really help. Is there a way to see all the matches that group(1) matched?

In the example below I'd like group(1) to print %one %two %three, not %three

(group(0) won't work because in the real world that's stuff after the initial match)

import sys
import re

line = '%one %two %three'
re_rule = re.compile("\s*(%\w+\s*)+")

result = re_rule.match(line)
if result:
    print("DBG:", result.group(1))
    print("DBG:", result.group(0))
import re

line = '%one %two %three'
re_rule = re.compile("%(\w+)")
f
result = re_rule.findall(line)

print(result)

Output is:

['one', 'two', 'three']

The trick here is using findall . https://docs.python.org/3/library/re.html#re.regex.findall

The re engine only keeps the last iteration of repeated capturing groups, but we can circumvent this behaviour by encapsulating a repeating non-capturing group in a capturing group like

re_rule = re.compile("((?:%\w+\s*)+)")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM