scandalous
scandalous

Reputation: 912

What is a RegEx to find phone numbers in Python?

I am trying to make a regex in python to detect 7-digit numbers and update contacts from a .vcf file. It then modifies the number to 8-digit number (just adding 5 before the number).Thing is the regex does not work.

I am having as error message "EOL while scanning string literal"

regex=re.compile(r'^(25|29|42[1-3]|42[8-9]|44|47[1-9]|49|7[0-9]|82|85|86|871|87[5-8]|9[0-8])/I s/^/5/')

#Open file for scanning
f = open("sample.vcf")

#scan each line in file
for line in f:
    #find all results corresponding to regex and store in pattern
    pattern=regex.findall(line)
#isolate results
    for word in pattern:
        print word
        count = count+1 #display number of occurences
        wordprefix = '5{}'.format(word)
        s=open("sample.vcf").read()
        s=s.replace(word,wordprefix)
        f=open("sample.vcf",'w')
        print wordprefix
        f.write(s)
        f.close()       

I am suspecting that my regex is not in the correct format for detecting a particular pattern of numbers with 2 digits which have a particular format like the 25x and 29x and 5 digits that can be any pattern of numbers.. (TOTAL 7 digits)

can anyone help me out on the correct format to adopt for such a case?

Upvotes: 0

Views: 1019

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213321

/I is not how you give modifiers for regex in python. And neither you do substitution like s///.

You should use re.sub() for substitution, and give the modifier as re.I, as 2nd argument to re.compile:

reg = re.compile(regexPattern, re.I)

And then for a string s, the substitution would look like:

re.sub(reg, replacement, s)

As such, your regex looks weird to me. If you want to match 7 digits numbers, starting with 25 or 29, then you should use:

r'(2[59][0-9]{5})'

And for replacement, use "5\1". In all, for a string s, your code would look like:

reg = re.compile(r'(2[59][0-9]{5})', re.I)
new_s = re.sub(reg, "5\1", s)

Upvotes: 1

Related Questions