Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find all digits between a character in python

Tags:

python

regex

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?

like image 595
bettas Avatar asked Jul 27 '15 10:07

bettas


1 Answers

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

or

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

This should do it for you.

like image 188
vks Avatar answered Sep 29 '22 00:09

vks