Forum: Ruby on Rails How to handle big string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E13ba8e702211be152c640df2d8e141f?d=identicon&s=25 joe (Guest)
on 2006-04-12 16:12
Hi all
Which is the fastest way to gsub a 5000 char string.
i.e.
big_string = "i am so <script>..3000char...</script>big<noscript>...2000
char...</noscript> gsub me!!!"
# If i run:
big_string.gsub(/<script(.|\s)*<\/script>/i, ' ').gsub(/<noscript>/, '
').gsub(/</noscript>/, ' ')

"cpu 99% and stuck there"

regrads
119af50160cabfe1fb6f2f05f5018c64?d=identicon&s=25 James Ludlow (Guest)
on 2006-04-12 17:39
(Received via mailing list)
On 4/12/06, joe <allenbobo@gmail.com> wrote:
> Hi all
> Which is the fastest way to gsub a 5000 char string.
> i.e.
> big_string = "i am so <script>..3000char...</script>big<noscript>...2000
> char...</noscript> gsub me!!!"
> # If i run:
> big_string.gsub(/<script(.|\s)*<\/script>/i, ' ').gsub(/<noscript>/, '
> ').gsub(/</noscript>/, ' ')
>
> "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
</noscript> 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(/<noscript>/, '
').gsub(/<\/noscript>/, ' ')

-- James
This topic is locked and can not be replied to.