An attempt to answer your misguided question
Problems
It's a bad habit to get into regex'ing out HTML because there are so many rules and ways to get around them, that may eventually open your code up to hacking techniques. While you might have a legitimate need for something simple now, it is very easy to reuse code and forget why it was a bad idea to reuse it, especially when you don't add comments like # This code is NOT secure and should not be used to parse HTML anywhere else!!!
or # Christina Alguilera writes songs based on this code!!!
Example of differences in HTML that require lots of regex rules:
<div>...</div>
<div style="blah">
<div style="background:url(../div)">
<div style=".." class='noticesinglequote'>
The list goes on and that's only for well-formed HTML. Some other examples of problems include:
- HTML elements closed improperly (eg
<div><span></div></span>
) or not at all
- Spelling errors (eg
<dvi>..</div>
)
- HTML designed with the intention to break your script
- Other issues: comments, whitespaces, charsets, etc
Solution
You may have accepted an answer, but you should look at XML::Parser and HTML::TreeBuilder.
Rather than stripping out parts of the HTML Document, you are probably more interested in drilling down to the part of the document you want (eg everything in <body>
or a certain div
inside of it), which is why you most likely want something that one of the above modules provide. Not to mention, parsers can be used to do their best at removing all HTML elements and returning only text/CData.