find all digits between a character in python
This might be an easy question but I can not get the result that I want!
I have a text like the one below:
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.»'
I am using regex in python and I want to get all the numbers between these characters "«" "»" that is 1. 2. and 3.
I tried something like this:
test = re.findall(r'[«.*?](d+)[.*?»]', text, re.DOTALL)
or this:
patt = re.findall(r'«.*?(d+).*?»', text, re.DOTALL)
and many others but none of them return what I want. What am I doing wrong?
Both patterns just return the number 1 without any other digit. What about number 2 and 3?
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"«([sS]*?)»",text)[0])
or
print re.findall(ur"d+","n".join(re.findall(ur"«([sS]*?)»",text)))
This should do it for you.
链接地址: http://www.djcxy.com/p/87112.html上一篇: wxPython:捕捉全球关键事件