[ACCEPTED]-Python Regex "object has no attribute-regex
Your exception means that urla has a value 6 of None. Since urla's value is determined 5 by the re.search call, it follows that re.search 4 returns None. And this happens when the 3 string doesn't match the pattern.
So basically 2 you should use:
urla = re.search(pattern2, match) if urla is not None: print filename, urla.group(1)
instead of what you have 1 now.
The reason for
TypeError is that
match usually return 5 either a
MatchObject or a
None. Only one of these has a 4
group method. And it's not a
None. So you need to 3 do:
url = re.search(pattern2, match) if url is not None: print(filename, url.group(0))
P.S. PEP-8 suggests using 4 spaces for indentation. It's 2 not just an opinion, it's a good practice. Your 1 code is fairly hard to read.
I got the same problem.
Using python2.6, you 1 can solve it in this way:
for match in matches: if len(match) > 0: urla = re.search(pattern2, match) try: urla.group(1): print filename, urla.group(1) excpet: print "Problem with",pattern2 urlb = re.search(pattern3, match) try: urlb.group(1) print filename, urlb.group(1) except: print "Problem with",pattern3
Please also note your mistaken assumption 6 that the error was in the third match, when it 5 was in fact in the second. This seems to 4 have led to the mistaken assumption that 3 the second match was doing something to 2 invalidate the third, sending you way off 1 track.
More Related questions