Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: regex clarification

by Enlil (Parson)
on Mar 05, 2005 at 10:33 UTC ( #436878=note: print w/ replies, xml ) Need Help??


in reply to regex clarification

What is happening is that the regex engine tries real hard to succeed. It greedily will grab as much as it can, and then back off if it means it can still succeed.

In the first row it matches the start of your line then David, but then it notices that David is followed by whitespace and then Vet, so this is a failure. So it backs up a letter and tries again. So now it matches Davi which is not followed by whitespace followed by Vet, so it is a successful match and as such you put Davi into the array. With Jackie it does not fail so it does not backup any letters just puts Jackie into the array, and then finally on the final line it tries Karen finds that it fails backs up a letter for Kare which of course succeeds for the same reason that Davi succeeded above, and viola you get the results you see.

FWIW here is one to do what intended to do in the first place with a regex:

while (<>) { push(@array, $&) if m/^(?>\w+)(?!\s+Vet)/; } print("@array\n");
Though that is only one way, and probably not the most efficient.

-enlil


Comment on Re: regex clarification
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2014-10-01 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (30 votes), past polls