[Tutor] Regex help please.

Nicole Seitz Nicole.Seitz@urz.uni-hd.de
Tue, 13 Aug 2002 16:14:44 +0000


>
> import re
> import os
>
> list =3D os.listdir('.') #lists all html documents in this directory
> input =3D open(list[0], "rb") #this will be changed to iterate over the=
 list
> text =3D input.read()
> p =3D re.compile("\?(\d+).htm", re.M)
> result =3D p.match(text)
>
>
> Now the last two line were written to test the search pattern
> "\?(\d+).htm". This will be changed to something like
> re.sub("\?(\d+).htm","\\1.html",text) later to do onestep swapping.
>
> But my problem is that I get the following output:
> >>>print result
>
> None
>
>
> So it seems like it is not traversing the file text and matching the
> pattern.

The 'match' function tries to match a pattern against the BEGINNING of a =
given=20
string. So, you have to use 'search' or 'findall'.

See

http://py-howto.sourceforge.net/regex/node22.html

Hope this helps!



Nicole