From: Paul Battley Date: 2006-07-07T19:55:04+09:00 Subject: Re: Regexp On 07/07/06, Fred VD wrote: > @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" + and * are greedy. Use +? and *? for non-greedy operation: a = 'a b c d e f g' a.gsub(%r!.+?!, 'ok') => "a b ok d ok f g" a.gsub(%r!.+!, 'ok') => "a b ok f g" A greedy expression will match up to the *last * occurrence of what follows it in the expression, whereas a non-greedy expression will match up to the *first* occurrence. Paul.