Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: perl regex with '\s+'

by TheHobbit (Pilgrim)
on Jun 08, 2002 at 17:28 UTC ( [id://172809]=note: print w/replies, xml ) Need Help??


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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://172809]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found