Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://436878]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-05-22 23:45 GMT
Find Nodes?
    Voting Booth?