Find Usa Phone Numbers in Python Script

Find USA phone numbers in python script

If you are interested in learning Regex, you could take a stab at writing it yourself. It's not quite as hard as it's made out to be. Sites like RegexPal allow you to enter some test data, then write and test a Regular Expression against that data. Using RegexPal, try adding some phone numbers in the various formats you expect to find them (with brackets, area codes, etc), grab a Regex cheatsheet and see how far you can get. If nothing else, it will help in reading other peoples Expressions.

Edit:
Here is a modified version of your Regex, which should also match 7 and 10-digit phone numbers that lack any hyphens, spaces or dots. I added question marks after the character classes (the []s), which makes anything within them optional. I tested it in RegexPal, but as I'm still learning Regex, I'm not sure that it's perfect. Give it a try.

(\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4})

It matched the following values in RegexPal:

000-000-0000
000 000 0000
000.000.0000

(000)000-0000
(000)000 0000
(000)000.0000
(000) 000-0000
(000) 000 0000
(000) 000.0000

000-0000
000 0000
000.0000

0000000
0000000000
(000)0000000

US-format phone numbers to links in Python

Nice first take :) I think this version is a bit more readable (and probably a teensy bit faster). The key thing to note here is the use of re.sub. Keeps us away from the nasty match indexes...

import re

PHONE_RE = re.compile('([(]{0,1}[2-9]\d{2}[)]{0,1}[-_. ]{0,1}[2-9]\d{2}[-_. ]{0,1}\d{4})')
NON_NUMERIC = re.compile('\D')

def numbers2links(s):

def makelink(mo):
raw_number = mo.group()
number = NON_NUMERIC.sub("", raw_number)
return '<a href="tel:%s">%s</a>' % (number, raw_number)

return PHONE_RE.sub(makelink, s)

print numbers2links("Ghost Busters at (555) 423-2368! How about this one: 555 456 7890! 555-456-7893 is where its at.")

A note: In my practice, I've not noticed much of a speedup pre-compiling simple regular expressions like the two I'm using, even if you're using them thousands of times. The re module may have some sort of internal caching - didn't bother to read the source and check.

Also, I replaced your method of checking each character to see if it's in string.digits with another re.sub() because I think my version is more readable, not because I'm certain it performs better (although it might).

Python code to accept many different formats of US phone numbers?

take only the numbers with a regex. then find out if they appended the 1 (NO area code starts with 1). if it's there, remove it otherwise, format the 10 digits the way you want.

import re
pnumber = re.sub("[^0-9]", "", input_number)
if pnumber[0] == 1:
pnumber = pnumber[1:] #strip 1st char if 1

#insert the dashes
if len(pnumber) == 10:
pnumber = "%s-%s-%s" % (pnumber[:3],pnumber[3:6],pnumber[6:])
else:
#throw error

Python Regex: US phone number parsing

You can do this:

import re
r = r'\((\d{3})\)\s*?(\d{3})\-(\d{4,5})'
l = ['(916) 111-11111', '(916)111-1111 ', ' (916) 111-1111', '916-111-1111', '(916)111 -1111', '( 916)111-1111', '(abc) 111-11i1']
print([re.findall(r, x) for x in l])

# [[('916', '111', '11111')], [('916', '111', '1111')], [('916', '111', '1111')], [], [], [], []]

Separate national and international phone numbers that are not formatted

If you don't know which numbers are international and which are local, you'll just have to try both:

def guess_phonenumber(clean, loc):
# Try national
pn = phonenumbers.parse(clean, loc)
if not phonenumbers.is_valid_number(pn):
# Not national; add + and try international
pn = phonenumbers.parse("+" + clean, None)
if not phonenumbers.is_valid_number(pn):
# Not international either
pn = None
return pn

guess_phonenumber(clean_phone_number, "BR")
# => PhoneNumber or None

If the phone cannot be recognised, it is likely either invalid altogether, or it is missing too much information to be able to be reconstructed (e.g. a local number, when you do not know which area it is local to).

Regular expression to validate US phone numbers using Formik and Yup

Try this regex it can meet your requirements

 /^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$/

It would look something like that:

validationSchema={Yup.object({
AdministratorCell: Yup.string()
.matches(/^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$/, {
message: "Invalid phone number",
excludeEmptyString: false,
})
})}


Related Topics



Leave a reply



Submit