... 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.)
Update:>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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |