From: dblack@... Date: 2006-07-07T19:59:43+09:00 Subject: Re: Regexp ---2049402039-232655781-1152269981=:16931 Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-232655781-1152269981=:16931" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---2049402039-232655781-1152269981=:16931 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Fri, 7 Jul 2006, Pe=C3=B1a, Botp wrote: > fr fred: > # When I try this : > # > # @a =3D "blabla1 blabla1 test1<\math> blabla2 blabla2 > # test2<\math>" > # @b =3D @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 =3D "blabla1 blabla1 test1<\math> blabla2 blabl= a2 > irb(main):002:0" test2<\math>" > =3D> "blabla1 blabla1 test1 blabla2 blabla2 \ntest2" > irb(main):003:0> @b =3D @a.gsub(/.+<\math>/,'ok') > =3D> "blabla1 blabla1 ok blabla2 blabla2 \nok" > > (ignore the newline; i just copiednpaste fr your email...) Interesting -- it actually works only *because* of the newline :-) With the newline, the .+ starts again on the second line (since . doesn't match a newline). Without it, the greed of .+ consumes everything up to the second <\math>. So if it's really on two lines, the non-greedy .+ isn't needed. But it's probably a good idea anyway, as it will work in a larger number of cases. David --=20 "To fully realize the potential of Rails, it's crucial that you take the time to fully understand Ruby--and with "Ruby for Rails" David has provided just what you need to help you achieve that goal." -- DAVID HEINEMEIER HANSSON, in the foreword to RUBY FOR RAILS. Complete foreword & sample chapters at http://www.manning.com/black! ---2049402039-232655781-1152269981=:16931-- ---2049402039-232655781-1152269981=:16931--