using re.finditer()

Erik Johnson spam at nospam.org
Wed Oct 27 17:30:26 EDT 2004


Robert Brewer wrote:

>Embed the flag(s) you desire in the regex itself. For example, to
>include DOTALL, change r'<td.*?>(.*?)</td>' to r'(?s)<td.*?>(.*?)</td>'

Ahhhh! :) Sorry, my bad. Its right there in the docs, but I missed it -
haven't fully comprehended all of re yet. :)


Peter Otten wrote:

>r = re.compile(r'<td.*?>(.*?)</td>', re.DOTALL)
>for match in r.finditer(html):
>    print match.group(1)

Good - perhaps a more obvious way to do it.

So there's two good work-arounds.
Thank you both for your helpful replies! :)

I am still left puzzled though, why this won't work:

pat = r'<td.*?>([\n.]*?)</td>'
for match in re.finditer(pat, html):
  print match.group(1)

but this will:

pat = r'<td.*?>([\w\W]*?)</td>'
for match in re.finditer(pat, html):
  print match.group(1)

Thanks,
-ej





More information about the Python-list mailing list