我在找有助于切丝的图案。字符串是这样的:
text = '1. first slice 2. second slice 3. slice number 3 4. the next one
5 that will not work but belong to no four 5. and this should be 5 and
so one...'
我想得到这个:
我希望你有这个想法。你知道吗
到目前为止,我所研究的是我可以使用这个:
import re
parts = re.findall("\d\\. \D+", text)
在遇到一个数字之前,它一直工作得很好。 我知道\D表达式是非数字的,我尝试使用:
parts = re.findall("\d\\. .+,text)
或者
parts = re.findall("(\d\\.).*,text)
还有很多,但我找不到合适的。你知道吗
我将感谢你的帮助。你知道吗
这应该管用
Regex Demo
正则表达式分解
Python代码
Ideone Demo
你可以使用消极的前瞻:
它匹配一个数字和一个点,后跟任何东西,前提是任何数字后面都不直接跟一个点。你知道吗
演示:
在线演示https://regex101.com/r/kF9jT1/1;为了模拟
re.findall()
行为,我添加了一个额外的(..)
和g
标志。你知道吗只是基于
lookahead
进行拆分。你知道吗输出:
['1. first slice', '2. second slice', '3. slice number 3', '4. the next one\n 5 that will not work but belong to no four', '5. and this should be 5 and\n so one...']
相关问题 更多 >
编程相关推荐