in reply to perl regex with '\s+'

Hi,
As is frequently the case, the problem cames from misunderstunding greadines of RE + and * operators. Let us take

/\s+(.*)\s+(.*)/
which is a simpler version of your RE, and try and match it against "___abc___def" (the "_" represents spaces). First, perl matches \s+ against the three spaces, and the first (.*) against all the other acaracters (that is "abc___def". But now there is a \s+, which require at least a space, so the first .* grundly gives back one character at time, until it only matches "abc__" (two spaces). Now \s+ matches this space, and the second (.*) is free to match "def". After that, $1 is "abc__", and $2 is "def".

I hope this explain things...

Cheers


Leo TheHobbit

Replies are listed 'Best First'.
Re: perl regex with '\s+'
by emcb (Beadle) on Jun 09, 2002 at 01:32 UTC

    Thanks fellow monks. I'm still learning re's so that got me. And i think i'll start using the 'magic' open on these ones...

    Elfyn

      \s+ change consecutive spaces to single spaces \s* remove all spaces altogether;