在python中查找字符之间的所有数字

2024-04-25 14:05:39 发布

您现在位置:Python中文网/ 问答频道 /正文

这可能是一个简单的问题,但我不能得到我想要的结果!你知道吗

我有一个文本如下:

text = 'To get to the destination follow this direction
1. First turn left and then
text text text
2. Secondly you have to some text here
some text here

For the second instruction follow the text below:
«1. some text some text some text
text text text.
2. some text some text text text text.
3. some text some text text text text.»'

我在python中使用regex,希望得到这些字符“«”“»”之间的所有数字,即1。2和3。你知道吗

我试过这样的方法:

test = re.findall(r'[«.*?](\d+)[.*?»]', text, re.DOTALL)

或者这个:

patt = re.findall(r'«.*?(\d+).*?»', text, re.DOTALL)

还有很多人,但没有一个能满足我的要求。我做错什么了?你知道吗

两种模式都只返回数字1而不返回任何其他数字。2号和3号呢?你知道吗


Tags: thetotext文本regethere数字
1条回答
网友
1楼 · 发布于 2024-04-25 14:05:39
text = '''To get to the destination follow this direction
1. First turn left and then
text text text
2. Secondly you have to some text here
some text here

For the second instruction follow the text below:
«1. some text some text some text
text text text.
2. some text some text text text text.
3. some text some text text text text.»'''


print re.findall(ur"\d+",re.findall(ur"«([\s\S]*?)»",text)[0])

或者

print re.findall(ur"\d+","\n".join(re.findall(ur"«([\s\S]*?)»",text)))

这应该能帮你。你知道吗

相关问题 更多 >