Python Forum
regex.findall that won't match anything
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regex.findall that won't match anything
#1
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]
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Facing issue in python regex newline match Shr 6 1,334 Oct-25-2023, 09:42 AM
Last Post: Shr
  regex findall() returning weird result Radical 1 658 Oct-15-2023, 08:47 PM
Last Post: snippsat
  Failing regex, space before and after the "match" tester_V 6 1,203 Mar-06-2023, 03:03 PM
Last Post: deanhystad
  Regex pattern match WJSwan 2 1,286 Feb-07-2023, 04:52 AM
Last Post: WJSwan
  Python: re.findall to find multiple instances don't work but search worked Secret 1 1,235 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  Match substring using regex Pavel_47 6 1,462 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Match key-value json,Regex saam 5 5,442 Dec-07-2021, 03:06 PM
Last Post: saam
  Regex findall() NewBeie 2 4,308 Jul-10-2020, 12:19 PM
Last Post: DeaD_EyE
  re.findall HELP!!! only returns None Rusty 10 7,042 Jun-20-2020, 12:13 AM
Last Post: Rusty
  The "FindAll" Error BadWhite 6 4,407 Apr-11-2020, 05:59 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020