[Tutor] string matching and replacement
Melissa Holden
Melissa Holden <melissa@thinice.com>
07 Sep 2000 14:33:19 -0500
I am trying to learn Python, and have used it for a few cgi scripts, but =
am still very much a beginner. Any help with this problem would be =
appreciated:
I am trying to search a file for 2 markers, and replace the text in =
between them. My first thought was to use regular expressions, but I =
haven't been able to get it to work. So far, I have come up with =
something like this:
p =3D re.compile(r'<!-- beginning text -->')
m =3D p.search( file )
if m:
print 'Match found: ', m.group()
else:
print 'No match'
As it is, this works, but when I try to add something in the middle and =
the end text marker (e.g., p=3Dre.compile(r'<!-- beginning text -->.*<!-- =
ending text -->')), it breaks. If I could get this part to work, I was =
planning to use sub to replace the section with something else. Basically,=
I want to find the beginning and ending tags, and replace what's in =
between them (which will vary in length).
Any help will be greatly appreciated.
Thanks!
Melissa