1 回答

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊
您可以使用正向先行表達(dá)式來(lái)匹配,直到下一個(gè)匹配。
這是更新的正則表達(dá)式(沙箱):
\b(?:\d+(?:\.\d+)?)\b.*?(?=\b(?:\d+(?:\.\d+)?)\b|$)
在Python中:
regex = r'\b(?:\d+(?:\.\d+)?)\b.*?(?=\b(?:\d+(?:\.\d+)?)\b|$)'
string = ' 1 Something Interesting here 2 More interesting text 2.1 An example of 2C19 a header 2.3 Another header example 2.4 another interesting header 10.1 header stuff 14 the last interesting 3A4 header'
result = re.findall(regex, string)
在這種情況下,result將是:
>>> result
['1 Something Interesting here ',
'2 More interesting text ',
'2.1 An example of 2C19 a header ',
'2.3 Another header example ',
'2.4 another interesting header ',
'10.1 header stuff ',
'14 the last interesting 3A4 header']
請(qǐng)注意,此解決方案還會(huì)提取末尾的間距。如果你不想要這個(gè)間距,你可以調(diào)用strip你的字符串:
>>> [ match.strip() for match in result ]
['1 Something Interesting here',
'2 More interesting text',
'2.1 An example of 2C19 a header',
'2.3 Another header example',
'2.4 another interesting header',
'10.1 header stuff',
'14 the last interesting 3A4 header']
添加回答
舉報(bào)