Python Forum

Full Version: regex.findall that won't match anything
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi, I am not sure why my regex code is not working for finding Malaysian phone number, can anyone helps to check? would be highly appreciate. When I use .findall method, it won't appear any group in the list.[Image: view?usp=sharing]
If +60 is for Malaysia, then this should return a result:
import re

regex = re.compile(r"\+60[0-9 ]+")

# then regex.findall(BIG_TEXT_WITH_MANY_NUMBERS)
If you need something with more functionality, you can look for https://pypi.org/project/phonenumbers/
It can parse phone numbers and you can access the country_code:

import phonenumbers


while True:
    phone_number = phonenumbers.parse(input("Phone number: "))
    country_code = phone_number.country_code
    region = phonenumbers.region_code_for_country_code(country_code)
    print(country_code, region)
A user tends to input invalid data. If phonenumbers have problems to parse a number, it will raise the NumberParseException. You can catch this.

But this is only usable, if you know where the numbers are.
If you want to scrape the web for phone numbers, you need a much better regex.