Forum: Ruby on Rails Multiple Characters Negate Using Regexp

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.
F68e4842b0a5487678e26046fdb2fdf3?d=identicon&s=25 softwareengineer 99 (Guest)
on 2006-02-20 00:54
(Received via mailing list)
Dear experts,

  I am trying to build a regular expression to filter out anything
between <script ... > and </script> tags where I can  specify something
using negate class to exclude more than one character  in sequence.

  I tried:

  originalresponse.gsub(/<script([^>]+)>([^<]+)?<\/script>/,'')

  but obviously if the script has the character < before </script> then
my regexp breaks.

  I also tried:
  originalresponse.gsub(/<script([^>]+)>([^<\/script]+)?<\/script>/,'')

  but it doesn't work


  Can anyone guide me on how I can specify multiple character negation?
  I will greatly appreciate it.

  Is this even possible? How else can I remove everything contained
between <script> tags?

  Thanks
  Frank
F68e4842b0a5487678e26046fdb2fdf3?d=identicon&s=25 softwareengineer 99 (Guest)
on 2006-02-20 01:16
(Received via mailing list)
Is it possible to specify a multiple line regular expression?

  Thanks for your assistance.
  Frank
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2006-02-20 04:06
Can you restate your original problem a bit better?  Are you wanting to
delete everything between the <script> and </script> tags?

If so, I think this expression should work (famous last words :-)

/(< *script.*?>).*?(<\/ *script *>)/

I tried it like this:

irb(main):001:0> "hello <script>goodbye</script> there".gsub(/(<
*script.*?>).*?(<\/ *script *>)/,'')
=> "hello  there"


Jeff
www.softiesonrails.com
5c2285f675ad99d67216b8d4f547ac8a?d=identicon&s=25 Jamie Macey (Guest)
on 2006-02-21 20:14
(Received via mailing list)
I recommend /<script([^>]+)>.*?<\/script>/

Adding the ? after the * makes it un-greedy, so it will match the
first following tag, as in the following example.

irb(main):001:0> s = "I have a <script foo> tag > inside < me
</script> don'cha know </script> :)"
=> "I have a <script foo> tag > inside < me </script> don'cha know
</script> :)"
irb(main):002:0> r = /<script([^>]+)>.*?<\/script>/
=> /<script([^>]+)>.*?<\/script>/
irb(main):003:0> s.gsub(r,'')
=> "I have a  don'cha know </script> :)"

- Jamie
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-02-24 13:37
(Received via mailing list)
softwareengineer 99 wrote:

> Is it possible to specify a multiple line regular expression?

yes, just put 'm' after it, e.g.

   %r{<textarea[^>]*?id="markup"[^>]*>([^<]*)}m

or

   /<textarea[^>]*?id="markup"[^>]*>([^<]*)/m

regards

   Justin
This topic is locked and can not be replied to.