From: Matthew Smillie Date: 2006-07-07T20:08:15+09:00 Subject: Re: Regexp On Jul 7, 2006, at 11:52, Pe�a, Botp wrote: > fr fred: > # When I try this : > # > # @a = "blabla1 blabla1 test1<\math> blabla2 blabla2 > # test2<\math>" > # @b = @a.gsub(/.+<\math>/,'ok') > # > # I get "blabla1 blabla1 ok" instead of "blabla1 blabla1 ok blabla2 > # blabla2 ok" > # > # Could you please help me to tune my regexp so it works the > # way I want ? > > your code works fine here, > > irb(main):001:0> @a = "blabla1 blabla1 test1<\math> blabla2 > blabla2 > irb(main):002:0" test2<\math>" > => "blabla1 blabla1 test1 blabla2 blabla2 > \ntest2" > irb(main):003:0> @b = @a.gsub(/.+<\math>/,'ok') > => "blabla1 blabla1 ok blabla2 blabla2 \nok" > > (ignore the newline; i just copiednpaste fr your email...) I thought this might warrant some explanation in light of the later posts about greedy operators. The newline from the paste is what makes this work differently from the normal example - greedy expressions (already mentioned) aren't greedy across newlines unless you specify a multiline regex, so the newline is what made the regex appear correct, when it wouldn't be in a general case. # with newline a = "blabla1 blabla1 test1 blabla2 blabla2 \ntest2" a.gsub(/.+<\math>/,'ok') => "blabla1 blabla1 ok blabla2 blabla2 \nok" # sans newline b = "blabla1 blabla1 test1 blabla2 blabla2 test2" b.gsub(/.+<\math>/,'ok') => "blabla1 blabla1 ok" matthew smillie.