[Tutor] Regular Expression
Michael H. Goldwasser
goldwamh at slu.edu
Sat Dec 22 05:15:42 CET 2007
Que,
I haven't tested the script, but please note that patt.match(line)
will only succeed when the pattern is at the start of the line. Use
patt.search(line) if you want to find the pattern anywhere within the
line.
Based on your desired highlight, you might want to use the pattern,
patt = re.compile(r"~02(\d*)~")
Also, your outfile.write command below doesn't provide any substitute
for %s. Presumably you mean outfile.write("%s\n" % m.group(1)) .
Good luck,
Michael
On Friday December 21, 2007, Que Prime wrote:
> I need to pull the highligted data from a similar file and can't seem to get
> my script to work:
>
> Script:
> import re
> infile = open("filter.txt","r")
> outfile = open("out.txt","w")
> patt = re.compile(r"~02([\d{10}])")
> for line in infile:
> m = patt.match(line)
> if m:
> outfile.write("%s\n")
> infile.close()
> outfile.close()
>
>
> File:
> 200~021111001491~05070
> 200~021111001777~05070
> 200~021111001995~05090
> 200~021111002609~05090
> 200~021111002789~05070
> 200~012~021111004169~0
> 200~021111004247~05090
> 200~021111008623~05090
> 200~021111010957~05090
> 200~02 1111011479~05090
> 200~0199~021111001237~
> 200~021111011600~05090
> 200~012~02 1111022305~0
> 200~021111023546~05090
> 200~021111025427~05090
More information about the Tutor
mailing list