This is not quite what BrowserUk's regex (or /fred/ && /bill/ for that matter) matches.

The main advantage of the lookaheads over multiple regex is that it extends linearly rather than compounding.

#! perl -slw use strict; use List::Util qw[ shuffle ]; my @terms = qw[ the quick brown fox jumps over the lazy dog ]; my $re = join'', map "(?=^.*$_)", @terms; $re = qr/$re/; for( 1 .. 10) { my $input = join ' ', shuffle @terms; $input =~ $re and print "$input matched"; } __END__ C:\test>junk48 quick brown the dog over fox lazy jumps the matched the dog over jumps the quick lazy brown fox matched jumps brown fox lazy quick the over dog the matched jumps brown dog over the fox lazy the quick matched over dog jumps fox the brown the lazy quick matched dog fox lazy the the quick over brown jumps matched lazy over brown dog quick the fox jumps the matched jumps fox quick brown the over lazy dog the matched over dog the lazy jumps quick brown fox the matched dog over lazy quick the the jumps brown fox matched

That is considerably easier than constructing and trying all 350,000+ regex.

Another advantage is that it only takes a small tweak to deal with the situation where not just the ordering is uncertain, but also some terms may be omitted. With the nice side-effect that you can use capturing to find out what was matched because the captures will be returned in a consistent ordering:

#! perl -slw use strict; use List::Util qw[ shuffle ]; my @terms = qw[ the quick brown fox jumps over the lazy dog ]; my $re = join'', map "(?=^.*($_))?", @terms; $re = qr/$re/; for( 1 .. 10) { my $input = join ' ', (shuffle @terms)[ 1 .. 5 ]; my @found = $input =~ $re; $_ //= 'n/a' for @found; print "Found [ @found ]\nin:'$input'"; } __END__ C:\test>junk48 Found [ the n/a n/a fox n/a over the lazy dog ] in:'fox over lazy the dog' Found [ n/a n/a brown fox n/a over n/a lazy dog ] in:'fox lazy dog brown over' Found [ the quick brown fox n/a n/a the n/a dog ] in:'the quick dog fox brown' Found [ n/a n/a brown fox jumps over n/a lazy n/a ] in:'brown lazy over jumps fox' Found [ the quick brown n/a jumps n/a the lazy n/a ] in:'lazy the quick brown jumps' Found [ the quick n/a n/a jumps n/a the n/a dog ] in:'dog quick the the jumps' Found [ n/a quick brown fox jumps n/a n/a n/a dog ] in:'fox jumps quick brown dog' Found [ the n/a brown n/a n/a over the lazy dog ] in:'over lazy the dog brown' Found [ the n/a brown fox jumps n/a the n/a dog ] in:'the brown dog fox jumps' Found [ the quick n/a fox n/a over the lazy n/a ] in:'fox lazy over quick the'

The double matching of 'the' can be a good or bad thing depending upon your purpose.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: Regex match at the beginning or end of string by BrowserUk
in thread Regex match at the beginning or end of string by cyber-guard

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.