Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by blakem (Monsignor)
on Oct 24, 2001 at 21:35 UTC ( [id://121206]=note: print w/replies, xml ) Need Help??


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

Ah, but you dont *need* to sort by length... the regex is anchored at the end, so the pattern that matches first from left to right will *already be* the longest match. For instance, look at the following code:
#!/usr/bin/perl -wT use strict; my $text = 'fedcba'; $text =~ (/(a|ba|cba|dcba)$/); print "Match: $1\n"; =OUTPUT Match: dcba
It matches on the longest one, even though its at the end of the alternation.... thats because the regex engine works from left to right, and the first one that matches wins. That was the whole point of my post, sorry I wasn't more explicit.

-Blake

  • Comment on Re: Re: Re: Re:{2} Getting impossible things right (behaviour of keys)
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: Re: Re:{2} Getting impossible things right (behaviour of keys)
by demerphq (Chancellor) on Oct 25, 2001 at 00:11 UTC
    Yes I see it now. Leftmost longest. I omitted the implication of the $. I should have caught the hint.

    Good one. :-)

    Yves / DeMerphq
    --
    Have you registered your Name Space?

      Well, it was my last post of the night, and I skimped on the explanation so I could get some sleep ;-P

      This thread illustrates why I try to post complete, self-contained scripts. (i.e. sample input via __DATA__, printed output samples, etc) I think much of the confusion could have been avoided, if the original script had a set of expected inputs and outputs.

      -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found