Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: regex clarification

by Enlil (Parson)
on Mar 05, 2005 at 10:33 UTC ( [id://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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-16 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found