Regex mystery: matching <a> or <area>

Gustaf Liljegren gustafl at algonet.se
Sun Oct 14 21:25:08 EDT 2001


I'm trying to match HTML <a> or <area> elements with the 'href' attribute. 
For some reason, the regex can't find any match if I put a sample match in 
the context of something else. I only need to add a space before, as in the 
string 's3' below.

>>> re_link = re.compile(r'<(a|area)[^>]+href.*/?>', re.I | re.M)
>>> s1 = '<a href="mypage.html">'
>>> s2 = '<area coords="0,0,5,5" href="tiny.html">'
>>> s3 = ' <a href="space.html">'
>>> re.match(re_link, s1).group()
'<a href="mypage.html">'
>>> re.match(re_link, s2).group()
'<area coords="0,0,5,5" href="tiny.html">'
>>> re.match(re_link, s3).group()
Traceback (most recent call last):
  File "<pyshell#49>", line 1, in ?
    re.match(re_link, s3).group()
AttributeError: 'None' object has no attribute 'group'
>>> 



More information about the Python-list mailing list