找到python中一个字符之间的所有数字

时间:2015-07-27 10:11:55

标签: python regex

这可能是一个简单的问题,但我无法得到我想要的结果!

我的文字如下:

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中使用正则表达式,我希望得到这些字符之间的所有数字“«”“»”即1. 2.和3.

我试过这样的事情:

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

或者这个:

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

和其他许多人,但没有一个回归我想要的。我做错了什么?

两种模式只返回数字1而没有任何其他数字。 2号和3号怎么样?

1 个答案:

答案 0 :(得分:4)

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)))

这应该为你做。