# Regexp and stack overflow

**URL:** <https://www.ruby-forum.com/t/regexp-and-stack-overflow/87538>\
**Category:** Ruby\
**Created:** [March 27, 2006, 9:54am UTC](https://www.ruby-forum.com/t/regexp-and-stack-overflow/87538 "2006-03-27T09:54:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 27, 2006, 9:54am UTC](https://www.ruby-forum.com/t/regexp-and-stack-overflow/87538/1 "2006-03-27T09:54:21Z")

</div>

i’ve a regexp :  
SCRIPT\_RE=Regexp.new(’\<script[^\>]_\>((.|\n)(?!/script))_’,  
Regexp::EXTENDED, ‘N’)

which is supposed to strip out everything being inside :

it works well for some html file but crash over other with the following  
error message :  
RegexpError: Stack overflow in regexp matcher:  
/\<script[^\>]_\>((.|\n)(?!/script))_\</script\>/xn  
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\_RE=Regexp.new(’\<script[^\>]_\>((.|\n)(?!/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 :  
  
  
//

and also having some script inside divs of body :  
  
[…]

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.
