views:

24

answers:

1

I have a text file with html:

Blah, blah, blah

some text is here.

<div> something here

something else </body></html>

so far, if the tags are on one line this works:

textfile = open("htmlfile.txt", "r+")

text = textfile.read()

a = re.search('<div.+?<\/html>', text)

repstr = c.group(0)

text = text.replace(repstr, '', 1)

works fine, I don't have nested tags. But if the tags are on multiple lines, like the first example, it doesn't work! what can I use to test multiple lines?

A: 

By default, the dot doesn't match new lines. To make it match new lines, you need to compile the regex with the flag re.DOTALL, eg:

a = re.search('<div.+?<\/html>', text, re.DOTALL)

That being said, you really shouldn't use regex to parse HTML.

Do yourself a favor and use an XML parser like BeautifulSoup.

NullUserException
oh, I see. My project is fairly simple and doesn't really require Beautifulsoup, but ill keep that in mind. Thanks!
Jack S.