tags:

views:

96

answers:

1

I need to remove all the Java Script tags and the content in between and style tags from the HTML code of web pages.So far I've come up with this expression :

"(<[ \r\n\t]*script([ \r\n\t>]|>){1,}([ \r\n\t]|.)*?</[ \r\n\t]*script[ \r\n\t]*>)|(<[ \r\n\t]*noscript([ \r\n\t>]|>){1,}([ \r\n\t]|.)*?</[ \r\n\t]*noscript[ \r\n\t]*>)|(<[ \r\n\t]*style([ \r\n\t>]|>){1,}([ \r\n\t]|.)*?</[ \r\n\t]*style[ \r\n\t]*>)"

I use JRegex library to work with regular expressions. When I test it in any regex tester it works just fine, but once I run my program - it all crashes down with this error report:

Exception in thread "Thread-0" java.lang.StackOverflowError
    at java.util.regex.Pattern$BranchConn.match(Unknown Source)
    at java.util.regex.Pattern$BmpCharProperty.match(Unknown Source)
    at java.util.regex.Pattern$Branch.match(Unknown Source)
    at java.util.regex.Pattern$GroupHead.match(Unknown Source)
    at java.util.regex.Pattern$LazyLoop.match(Unknown Source)
    at java.util.regex.Pattern$GroupTail.match(Unknown Source)
    at java.util.regex.Pattern$BranchConn.match(Unknown Source)
    at java.util.regex.Pattern$CharProperty.match(Unknown Source)
    at java.util.regex.Pattern$Branch.match(Unknown Source)
    at java.util.regex.Pattern$GroupHead.match(Unknown Source)
    at java.util.regex.Pattern$LazyLoop.match(Unknown Source)
..................................

And it keeps on going forever. If anyone can give me an advice on this one - I'll be very grateful.

+1  A: 

Why not use an HTML parser and just remove the <script> and <style> nodes?

Skilldrick