regex.findall that won't match anything - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: regex.findall that won't match anything (/thread-29877.html) |
regex.findall that won't match anything - xiaobai97 - Sep-24-2020 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. RE: regex.findall that won't match anything - DeaD_EyE - Sep-24-2020 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. |