I'm new to python. I have a long string that has several numbers in it. I only need the numbers that matches the XX XX XXXXXX form (ex. 15 09 066456). Any help is appreciated.
Asked
Active
Viewed 48 times
1
-
regex is your friend. – Christian Sloper Oct 13 '20 at 16:18
-
Does this answer your question? [How to extract numbers from a string in Python?](https://stackoverflow.com/questions/4289331/how-to-extract-numbers-from-a-string-in-python) – Tomerikoo Oct 14 '20 at 09:48
1 Answers
2
I'm not allowed to comment, so posting the possible solution(using regex) here, python 3.x
import re
text='15 09 066456'
pattern = r'\d{2}\s\d{2}\s\d{6}'
match= re.findall(pattern,text)
print(match)

Abhinav Mathur
- 7,791
- 3
- 10
- 24

kartik sharma
- 113
- 5
-
-
-
1its working good but it only outputs only one XX XX XXXXXX code. How can I get more? Edit: Figured it out: `pattern = r'\d{2}\s\d{2}\s\d{6}' match = re.findall(pattern,text) print(match)` – xrtxn Oct 13 '20 at 17:01
-
1@Hackry As already suggested by you, re.findall will do the needful to find all the matching patterns. Same Edit is done in the answer above. Thanks – kartik sharma Oct 14 '20 at 08:40