From: Martin DeMello Date: 2006-07-07T19:54:36+09:00 Subject: Re: Regexp On 7/7/06, Fred VD wrote: > Hello, > > I'm new to Ruby and I'm fighting with regexp. > > I would like to replace 'test1<\math>' and 'test2<\math>' by > 'ok' in the string "blabla1 blabla1 test1<\math> blabla2 blabla2 > test2<\math>" > > 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 ? You need to replace .+ with .+? - the former is 'greedy'; i.e. it matches as much as it possibly can, so it matches everything between the first and the last <\math>. martin