http://www.perlmonks.org?node_id=121206


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