-2

I'm fairly new to python and I'm trying to understand I do I loop regex groups, ex:

reobj = re.compile('<a href="(.*?)">(.*?)</a>', re.IGNORECASE)
result = reobj.findall(body)

how do I loop the 2 groups from the regex ? Thanks!

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268

2 Answers2

6

Did you actually try this in the shell?

>>> body = """<a href="http://foo.com">Foo</a><br><a href="http://bar.com">Bar</a>"""
>>> reobj = re.compile('<a href="(.*?)">(.*?)</a>', re.IGNORECASE)
>>> result = reobj.findall(body)
>>> result
[('http://foo.com', 'Foo'), ('http://bar.com', 'Bar')]

So the result of findall is simply a list of tuples containing the matched groups. If you don't know how to iterate through a list, then you need to do an introductory Python tutorial.

[insert standard rant about how you shouldn't use regex to parse HTML here...]

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

The answer I needed was:

reobj = re.compile('<a href="(.*?)">(.*?)</a>', re.IGNORECASE)
result = reobj.findall(body)


for link in result:
        print link[0] + link[1]
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268