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

Re:{4} Getting impossible things right (behaviour of keys)

by jeroenes (Priest)
on Oct 24, 2001 at 14:25 UTC ( #121065=note: print w/ replies, xml ) Need Help??


in reply to Re: Re:{2} Getting impossible things right (behaviour of keys)
in thread Getting impossible things right (behaviour of keys)

Hmm.. from perlre:

Alternatives are tried from left to right, so the first alternative found for which the entire expression matches, is the one that is chosen. This means that alternatives are not necessarily greedy. For example: when matching `foo|foot' against "barefoot", only the "foo" part will match, as that is the first alternative tried, and it successfully matches the target string.

So that would take a string random with respect to length.

Jeroen

Update: I see. Can you explain that regex-feature?


Comment on Re:{4} Getting impossible things right (behaviour of keys)
Re: Re:{4} Getting impossible things right (behaviour of keys)
by blakem (Monsignor) on Oct 24, 2001 at 21:39 UTC
    Add the requirement that it has to be anchored at the end, and you will get the longest match. Try /(ot|foot|refoot)$/ and see which one matches. Bet ya its the longest one.

    -Blake

Re: Re:{4} Getting impossible things right (behaviour of keys)
by blakem (Monsignor) on Oct 25, 2001 at 01:18 UTC
    Reply to Update.

    Read the paragraph you quoted above very carefully.....

    The regex engine works something like this: it moves from left to right, checking each alternation in order before moving on to the next position in the string.

    Now, lets consider the pattern /(a|b|c)/ against the string 123abc. In the following diagram '^' denotes the current position in the string. The "pointer" gets moved one char to the right after each stanza:

    ($txt = '123abc') =~ /(a|b|c)/; 1. ^123abc - check for a -> fail - check for b -> fail - check for c -> fail 2. 1^23abc - check for a -> fail - check for b -> fail - check for c -> fail 3. 12^3abc - check for a -> fail - check for b -> fail - check for c -> fail 4. 123^abc - check for a -> succeess, $1 becomes 'a'
    Here are a few others, to illustrate the point.
    ($txt = 'barefoot') =~ /(foo|foot)/; 1. ^barefoot - check for foo -> fail - check for foot -> fail 2. b^arefoot - check for foo -> fail - check for foot -> fail 3. ba^refoot - check for foo -> fail - check for foot -> fail 4. bar^efoot - check for foo -> fail - check for foot -> fail 5. bare^foot - check for foo -> success, $1 becomes 'foo'
    Here, 'foo' beats 'foot' because they match at the same spot in the string, and foo is listed first in the alternation. The leftmost match will always win, the order they are listed in the alternation is merely a tie breaker.

    And the one in question (slightly shortened)..

    ($txt = 'arvec') =~ /(ar|ec|vec)$/ 1. ^arvec - check for ar$ -> fail (because of the end-of-string anch +or) - check for ec$ -> fail - check for vec$ -> fail 2. a^rvec - check for ar$ -> fail - check for ec$ -> fail - check for vec$ -> fail 3. ar^vec - check for ar$ -> fail - check for ec$ -> fail - check for vec$ -> success, $1 becomes 'vec'
    Notice how 'ec' *would* match in the next stanza ("arv^ec"), but we never get that far. The first match wins, and in this situation it is the one we want.

    Now, go back and read that paragraph you quoted again... does it make more sense now?

    -Blake

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-12-26 16:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls