我有以下简单的文字:
2 of 5 deliveries some text some text... 1. 3 of 5 items some text some text... 2. 1 of 5 items found in box some text...
现在,我希望基于数字[0.-9。]将文本拆分为以下内容:(每一行代表列表中的条目)。
2 of 5 deliveries some text some text...,
3 of 5 items some text some text...,
1 of 5 items found in box some text...
这是所需的输出。但是,它不能与regex
一起使用re.split('([0\.-9\.]+)', text)
。它始终仅以数字分隔。用Python进行转换的最聪明的方法是什么?
您可以使用以下模式:
>>> re.split(r'\s+\d+\.\s+', text)
['2 of 5 deliveries some text some text...',
'3 of 5 items some text some text...',
'1 of 5 items found in box some text...']
解释:
>>> re.split(r'''
\s+ # Matches leading spaces to the separator
\d+ # Matches digit character
\. # Matches '.' character
\s+ # Matches trailing spaces after the separator
''', text, flags=re.VERBOSE)
['2 of 5 deliveries some text some text...',
'3 of 5 items some text some text...',
'1 of 5 items found in box some text...']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句