André
André

Reputation: 25584

Python - Regular expression to find a phone number on a text

I'm getting some trouble to find a regular expression to find a phone number in this format that allways start with the number "9" in the middle of a text:

90 00 00 000

I can find a number with this regular expression:

phones= re.findall(r'(\d{2})\D+(\d{2})\D+(\d{2})\D+(\d{3})', "55 66 66 778 xcvxcvxv 94 44 44 456")
print phones
> [('55', '66', '66', '778'), ('94', '44', '44', '456')]

But I need to find only the number started witn "9". How can I do this?

Best Regards,

Upvotes: 0

Views: 1079

Answers (1)

OGHaza
OGHaza

Reputation: 4795

You can just change the first \d{2} to 9\d, however I'd recommend also changing it to match whitespace rather than non-numeric characters in between digits.

(9\d)\s+(\d{2})\s+(\d{2})\s+(\d{3})

Upvotes: 2

Related Questions