Python version of this Perl 4-line regexp script?
November 11, 2006 4:46 PM
Subscribe
Convert perl to python: I am very comfortable with regular expressions but I need help converting an implementation of them from Perl to Python. I have an example Perl script below that opens a file, scans through it one line at a time, does a s// replace, then finds a string in the result and stores it in another variable. I would love to just have a working Python version of this for my edification-- and if you have any "Perl to Python" tutorial links I'd love to see them.
This code doesn't do much but it's a prime example of the sort of cruft I write in Perl every day for little things. I know about re.match and re.search but how do I do the rest?
open(L,"/tmp/A");
while(<L>) {
s/\#\#\#//;
if(/^ {1,3}[0-9]{1,3}\. http\:\/\/(.*)/) { print $1; }
}
posted by neustile to computers & internet (11 comments total)
1 user marked this as a favorite
import re
for line in open("/tmp/A"):
> m = re.match(r"^ {1,3}\d{1,3}\. http://(.*)", line.replace("###", ""))
> if m is not None: print m.group(1)
posted by cmiller at 5:07 PM on November 11, 2006