phone number regex

Tony Johnson gjohnson at gs.verio.net
Sun Aug 27 22:28:48 EDT 2000


Thank You for the reply. Someone replied to this but I lost the email. 
But I have figured out my problem and I have just a small question.  I
created a regex that matched an area code ie. (555).  The regex is: 

bash-2.04$ cat wisp-test4
#!/usr/local/bin/python
 
import sys, string, re , fileinput
acode_tmpl = re.compile('[^\(\d][\D+][^\)\d]')
file = open(sys.argv[1], 'r')
for line in fileinput.input():
   line = string.strip(line)
   print line
   a = re.split(acode_tmpl,line)
   print a                

And it produces output like:

bash-2.04$ wisp-test4 networkg.txt
AB Calgary  (403)781-5200
['', '', '', '', '(403)781-5200']
AB Edmonton  (780)423-5600
['', '', '', '', ' (780)423-5600']
AB Lethbridge  (403)380-5325
['', '', '', '', '', '(403)380-5325']
AB Medicine Hat  (403)528-1900
['', '', '', '', '', '', '403)528-1900']
AB Red Deer  (403)309-1100
['', '', '', '', ' (403)309-1100']
AK Anchorage  907-868-7594
['', '', '', '', '  907-868-7594']
AK Juneau  907-463-2551
['', '', '', '  907-463-2551']
AL Anniston  256-238-9380
['', '', '', '', ' 256-238-9380']
AL Athens  256-233-3081


I would like my script to not be include the text before and after the
pattern match.  Is there a switch I turn n in the re.switch function or
do I have to break the match further from here?

Tony Johnson wrote:
> 
> This should be fairly easy but just wanted to ask.  I have an isp phone
> list file with lines formatted as such
> 
> State   City    555-555-555
> 
> I need to reformat the file.  I am trying to make a regex that will
> match the phone number field.
> 
> so i try to make a regex that will match the above phone number
> '[\d]+\-[\d]+\-[\d]+'  This makes the re.split function delete what
> matched just like awk , etc so can i just say '(^[\d]+\-[\d]+\-[\d]+)' ?




More information about the Python-list mailing list