# How to handle big string

**URL:** <https://www.ruby-forum.com/t/how-to-handle-big-string/93866>\
**Category:** Rails\
**Created:** [April 12, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/how-to-handle-big-string/93866 "2006-04-12T16:12:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [April 12, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/how-to-handle-big-string/93866/1 "2006-04-12T16:12:34Z")

</div>

Hi all  
Which is the fastest way to gsub a 5000 char string.  
i.e.  
big\_string = “i am so big…2000  
char… gsub me!!!”

# If i run:

big\_string.gsub(/\<script(.|\s)\*\</script\>/i, ’ ').gsub(//, ’  
').gsub(//, ’ ')

“cpu 99% and stuck there”

regrads

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [April 12, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/how-to-handle-big-string/93866/2 "2006-04-12T17:39:37Z")

</div>

On 4/12/06, joe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all  
> Which is the fastest way to gsub a 5000 char string.  
> i.e.  
> big\_string = “i am so big…2000  
> char… gsub me!!!”
> 
> # If i run:
> 
> big\_string.gsub(/\<script(.|\s)\*\</script\>/i, ’ ').gsub(//, ’  
> ').gsub(//, ’ ')
> 
> “cpu 99% and stuck there”

This should take less than a second to excute, so you likely have  
something else going wrong in your code.

Also, your regex is wrong. You aren’t escaping the / in the final  
 tag. That alternation you’re doing with the (.|\s)\* can  
be replaced by .\*? and telling the regex to span multiple lines with  
the ‘m’ option.

big\_string.gsub(/\<script.\*?\</script\>/im, ’ ').gsub(//, ’  
').gsub(/\</noscript\>/, ’ ')

– James
