[Tutor] string matching and replacement

Melissa Holden Melissa Holden <melissa@thinice.com>
07 Sep 2000 15:07:02 -0500


Thank you for your help.  This may be a dumb question, but is it possible =
to search a file?  For example, instead of string =3D '<!-- beginning text =
-->This is a comment<!-- ending text -->'
m =3D p.search(string)
I was trying to get it to search an external file that I had referenced =
earlier in the program by=20
file =3D open('../index5.html').read()

Thanks for your assistance!


On 9/7/00, Sean 'Shaleh' Perry <shaleh@valinux.com> wrote:
>$ python
>>>> import re
>>>> p =3D re.compile('<!-- beginning text -->(.*)<!-- ending text -->')
>>>> string =3D '<!-- beginning text -->This is a comment<!-- ending=20
>text -->'
>>>> m =3D p.search(string)
>>>> if m:
>...    print 'Match', m.group()
>... else:
>...   print 'No Match'
>...=20
>Match <!-- beginning text -->This is a comment<!-- ending text -->
>>>> m.groups() # returns a tuple of the matched groups
>('This is a comment',)
>
>There ya go (-:
>