python - Regex Capture Multiple Phrases after One -
i trying figure out how make regex capture bunch of items come after 1 particular thing. using python this. 1 example of using text b <4>.<5> <6> <1> m<2> . <3>
intent of capturing 1, 2, , 3. thought regular expression a.*?<(.+?)>
work, caputures final 3 using python re.findall
. can this?
the regex
module (going replace re
in future pythons) supports variable lookbehinds, makes easy:
s = "b <4>.<5> <6> a23 <1> m<2> . <3>" import regex print regex.findall(r'(?<=a\d+.*)<.+?>', s) # ['<1>', '<2>', '<3>']
(i'm using a\d+
instead of a
make thing interesting). if you're bound stock re
, you're forced ugly workarounds this:
import re print re.findall(r'(<[^<>]+>)(?=(?:.(?!a\d+))*$)', s) # ['<1>', '<2>', '<3>']
or pre-splitting:
print re.findall(r'<.+?>', re.split(r'a\d+', s)[-1])
Comments
Post a Comment