我有一个字符串,其中包含由特定子字符串分隔的组。就像是:
FOO10 F10 O20 FOO20 F11 O11
我想在保留组标识符的同时将其分成组,即
FOO10 F10 O20
FOO20 F11 O11
有没有办法捕获从单词 FOO 到下一个 FOO 的左边界或行尾的任何内容?
>>> import re
>>> re.findall('FOO.*?(?=FOO|$)', "FOO10 F10 O20 FOO20 F11 O11" )
['FOO10 F10 O20 ', 'FOO20 F11 O11']
>>> map(str.strip, re.findall('FOO.*?(?=FOO|$)', "FOO10 F10 O20 FOO20 F11 O11" ))
['FOO10 F10 O20', 'FOO20 F11 O11']
>>> s = "FOO10 F10 O20 FOO20 F11 O11"
>>> import re
>>> regex = re.compile(r"FOO(?:(?!\s*FOO).)*")
>>> regex.findall(s)
['FOO10 F10 O20', 'FOO20 F11 O11']
解释:
FOO # Match FOO
(?: # Start of non-capturing group:
(?!\s*FOO) # Assert that we're not right before (whitespace) FOO
. # Match any character
)* # Repeat as needed