1 回答
TA贡献2065条经验 获得超14个赞
您可以使用正向先行表达式来匹配,直到下一个匹配。
这是更新的正则表达式(沙箱):
\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']
请注意,此解决方案还会提取末尾的间距。如果你不想要这个间距,你可以调用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']
添加回答
举报