Home > Net >  Ignore character in Regex Python?
Ignore character in Regex Python?

Time:10-27

I want to find all phone numbers that start with a "0" and are followed by 10 digits.

r1=re.search('^[0] \d*{10}',i)

However, some of them are written as "01-234-567-8-90", which technically is a 0 followed by 10 digits, but it has dashes in it. When I run a Regex search command, it doesn't recognise it.

Is there a way to tell regex to ignore certain characters, such as " " and "-"?

Many thanks,

Mr Cezar

CodePudding user response:

I'm sure there's better methods, but you could just remove the hyphens before the regex search.

i.replace('-', ''))

CodePudding user response:

I think to validate it, you can remove "-" like

phone_number_without_hyphens = phone.replace("-", "")

then check it by regex

re.search('^[0] \d*{10}', phone_number_without_hyphens)
  • Related