Regular Expressions

Max spamhole at gmx.at
Wed Aug 25 15:50:05 EDT 2004


On Wednesday 25 August 2004 18:02, Oriana wrote:

Hi,

> I would like to write some sort of regular expression to replace all
> the text from the first dotted line up to the first asterisk that it's
> followed either by another asterisk or by another *----
> line.....please help, I don't know where to start!!!


Below is some code that will search for the starting and ending *--- lines in 
a specified file and print the whole file with the *--- part being removed.

Hope thats a start,
Max

#!/usr/bin/python

import re

f = file("somefile.c", "r")
text = f.read()
f.close()

m = re.search("\*-*.*\*-*\n", text, re.DOTALL)

if not m:
        print "nothing found"
else:
        print text[:m.start(0)] + text[m.end(0):]



More information about the Python-list mailing list