[Tutor] Expression matching
Daniel Yoo
dyoo@hkn.eecs.berkeley.edu
Wed, 9 May 2001 01:34:48 -0700 (PDT)
On Wed, 9 May 2001, Sharriff Aina wrote:
> Hi! could someone help me with my Regular expression? I=B4m trying to mat=
ch
> all occurring instances of the "<img>" tag in a big chunk of HTML.
>=20
> ## my code
> import re
> aa =3D '<span>ff<td><img src>sasa<b><img src=3D"ksksk.jpg">nn<br>blah<img
> src=3D"C:\\test\test.jpg>"'
> imagematch =3D re.compile('<img src=3D".*>')
> print imagematch.match(aa) ### results in 'None'
> ## another try
> testmatch =3D re.compile('<img?\s+.*>')
> print testmatch.match(aa) ### results also in 'None'
Try using testmatch.search(): there's a difference!
1. Matching can only work if what we're looking for is at the very
beginning.
2. Searching can start anywhere within the string.
The documentation mentions this briefly here:
http://python.org/doc/current/lib/matching-searching.html
but it's always a gotcha when you start off with Python regular
expressions. Try search(), and you should get better results.