#!/usr/bin/env python
import re
DataDir = '/home/david/Programming/Python'
phonefile = DataDir + '/' + 'PhoneNumbers.txt'
"""Read each line from file. If it starts with
a string of exactly 10 consecutive digits, assume
this is a phone number and print it."""
ph_nbr_pattern = r'^(\d{10})(?:\s|$)'
compile_obj = re.compile(ph_nbr_pattern)
file2read = open(phonefile, 'r')
for currentline in file2read:
match_obj = compile_obj.search(currentline)
if match_obj:
print currentline.rstrip()
file2read.close()
"""Output is:
4616186224
3501292628
2698109000
4398248508
8462632398
5414846117
9167449701
5097458418
"""
samready commented: thanks that did it. (you're right, the space wasn't supposed to be there, but i put it there so it didn't make a smiley :$" +0
lllllIllIlllI commented: Woops :) my bad. Nicely done +2
Venom Rush commented: Thanks for your help ;) +3