Forum: Ruby regexp and stack overflow

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-03-27 11:54
(Received via mailing list)
i've a regexp :'<script[^>]*>((.|\n)(?!/script))*</script>',
Regexp::EXTENDED, 'N')

which is supposed to strip out everything being inside :
<script ...>(part suppressed...)</script>

it works well for some html file but crash over other with the following
error message :
RegexpError: Stack overflow in regexp matcher:
method gsub
in check_files.rb at line 38
method stripHTML
in check_files.rb at line 38

ligne 38 being :

    self.gsub(SCRIPT_RE, '').gsub(TAGS_RE, '').gsub(/\s+/, '
').gsub(NBSP_RE, '')

with :'<script[^>]*>((.|\n)(?!/script))*</script>',
Regexp::EXTENDED, 'N')

what i want to do :

strip out all the contents of scripts, all the html tags with their
attributes, and also i have to add striping out any css declaration (not
done yet).

the prog failes for a file having the following parts for script :
                <script type="text/javascript"
src="Mac-roman-utf-8_fichiers/wikibits.js"><!-- wikibits js --></script>
                <script type="text/javascript"
src="Mac-roman-utf-8_fichiers/index.php"><!-- site js --></script>
                <style type="text/css">/*<![CDATA[*/
@import "/w/index.php?title=-&action=raw&gen=css&maxage=2678400";
/*]]>*/</style></head><body class="ns-0 ltr">

and also having some script inside divs of body :
        <script type="text/javascript"> if (window.isMSIE55) fixalpha();
        <script type="text/javascript"> if (window.runOnloadHook)

i can't make use of tidy for that purpose, because the reason to strip
out any kind of html, to keep the text only, is to help some prog
finding out the encoding of the file.
This topic is locked and can not be replied to.