On 7/7/06, Fred VD <outrelouxhe / yahoo.fr> wrote:
> Hello,
>
> I'm new to Ruby and I'm fighting with regexp.
>
> I would like to replace '<math>test1<\math>' and '<math>test2<\math>' by
> 'ok' in the string "blabla1 blabla1 <math>test1<\math> blabla2 blabla2
> <math>test2<\math>"
>
> When I try this :
>
>  @a = "blabla1 blabla1 <math>test1<\math> blabla2 blabla2
> <math>test2<\math>"
>  @b = @a.gsub(/<math>.+<\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 <math> and the last <\math>.

martin