... match the longest "or" possible ...

The Perl regex engine matches the leftmost longest substring for a given pattern; leftmost trumps longest. If you want to match the longest substring that occurs anywhere in the string (which, IIRC, is the POSIX definition of matching), you have to jump through a hoop or two:

>perl -wMstrict -le "my $s = 'x s m e f s f pl s f x'; ;; $s =~ qr/^.+(s m e f|s f pl|s f).+$/; print qq{'$1'}; print qq{ -------}; ;; my $alt = qr{ s[ ]m[ ]e[ ]f | s[ ]f[ ]pl | s[ ]x }xms; for my $s ( 'x s m e f s f pl s f x', 'x s f s f pl s m e f x', 'x s f s m e f s f pl x', ) { my ($longest) = reverse sort { length($a) <=> length($b) } $s =~ m{ (?<= .) $alt (?= .) }xmsg ; print qq{'$longest'}; } " 's f' ------- 's m e f' 's m e f' 's m e f'

I think there is another, possibly more succinct, solution using the 5.10+ Special Backtracking Control Verbs, but I can't remember it ATM.

Update: Well, this doesn't use backtracking control verbs quite the way I imagined, but at least it gets rid of the list-making and sorting:

>perl -wMstrict -le "my $alt = qr{ s[ ]m[ ]e[ ]f | s[ ]f[ ]pl | s[ ]f }xms; my $rex = qr{ (?<= .) $alt (?= .) }xms; ;; for my $s ( 'x s m e f s f pl s f x', 'x s f s f pl s m e f x', 'x s f s m e f s f pl x', 'x s f s f x', 'x s s x', 's f', 's m e f', ) { local our $longest; local our $offset; use re 'eval'; $s =~ m{ ($rex) (?{ ($longest, $offset) = ($1, $-[1]) if ! defined($longest) || length($1) > length($longest) }) (*SKIP)(*FAIL) }xmsg; print qq{'$s' -> '$longest' at $offset} if defined $longest; } " 'x s m e f s f pl s f x' -> 's m e f' at 3 'x s f s f pl s m e f x' -> 's m e f' at 16 'x s f s m e f s f pl x' -> 's m e f' at 8 'x s f s f x' -> 's f' at 3

Note that in the string  'x  s f  s f  x' the left-most occurrence of 's f' is found. This can be changed to the right-most by changing the comparison operator in  length($1) > length($longest) to >= instead.


In reply to Re: match longest sequence in an "or" RE by AnomalousMonk
in thread match longest sequence in an "or" RE by silentius

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.