For a list of things to match in any order, what I did was:
The way that works is that it matches (?:A|B|C)+, any of the three submatches can match, and then goes back to try for more.my $regexLockParams = '(?:[\\s,]+' ."|(?:$regexSubstringOf{angle}|$regexSubstringOf{offset})\\s*$rege +xNumber" ."|$regexSubstringOf{dispersion}\\s*$regexNumber" #."|..." .')+';
At the end of the match, you are left with $1 containing the angle, or undef if it was unspecified, and $2 containing the dispersion or again undef.
Order doesn't matter since the alternation is repeated.
IE:
angle 5, dispersion20 ==> $1=5 $2=20
disp1ang4 ==> $1=4 $2=1
d42 ==> $1=undef $2=42
CAVEAT:
Each alternation branch must fail to match before the capture is closed, otherwise that capture variable will be overwritten and not get restored to the previous value when the engine backtracks past the capture.
You can use a lookahead (?=...)to ensure that the remainder of the branch will match if necessary. (In the above example, the capture was last, so there was no postfix to worry about)
sub safeCapture { # Workaround for Regex issue in which backtracked captures inside +alternations inside repetition, will stomp on the capture value. my $prefix = shift; my $cap = shift; my $postfix = shift; return "$prefix($cap(?=$postfix))$postfix"; }
In reply to Re: pattern matching words in any order
by SuicideJunkie
in thread pattern matching words in any order
by daveatmcafee
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |