... match either pattern1pattern2 or pattern2pattern1 ...

What this (rather than the title) implies to me is "match either pattern1 immediately followed by pattern2, or else pattern2 immediately followed by pattern1". This is not quite what BrowserUk's regex (or  /fred/ && /bill/ for that matter) matches. (Sorry for the line-wrap.)

>perl -wMstrict -le "my @strings = qw(nowknow knownow know now no); ;; my $pattern1 = qr{ now }xms; my $pattern2 = qr{ know }xms; ;; my $regex1 = qr{ (?= \A .*? $pattern1) (?= \A .*? $pattern2) }xms; my $regex2 = qr{ $pattern1 $pattern2 | $pattern2 $pattern1 }xms; ;; for my $regex ($regex1, $regex2) { print qq{for regex $regex}; for my $string (@strings) { print qq{ '$string' has }, $string =~ $regex ? 'a' : 'NO', ' match'; } } " for regex (?msx-i: (?= \A .*? (?msx-i: now )) (?= \A .*? (?msx-i: kno +w )) ) 'nowknow' has a match 'knownow' has a match 'know' has a match 'now' has NO match 'no' has NO match for regex (?msx-i: (?msx-i: now ) (?msx-i: know ) | (?msx-i: know ) ( +?msx-i: now ) ) 'nowknow' has a match 'knownow' has a match 'know' has NO match 'now' has NO match 'no' has NO match
Update:

In general, the key consideration is not the 'length' of the regex. It's not hard to write a regex of a couple hundred characters that will run for the rest of your life, even if you live to be as old as Methuselah. Conversely, a regex of several thousand characters can run quickly (depending on your definition of 'quick').

One important speed consideration is to reduce the possible starting points in a string from which a match may be attempted. That's what the  ^ and  \A anchors do: a match may only occur at the start of the string.

Another approach is to minimize backtracking by making a regex or regex sub-patterns 'atomic' with the  (?>pattern) construct. See Extended Patterns. See also perlretut and perlrequick.


In reply to Re^2: Regex match at the beginning or end of string by AnomalousMonk
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.