For a list of things to match in any order, what I did was:

my $regexLockParams = '(?:[\\s,]+' ."|(?:$regexSubstringOf{angle}|$regexSubstringOf{offset})\\s*$rege +xNumber" ."|$regexSubstringOf{dispersion}\\s*$regexNumber" #."|..." .')+';
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.
In this case, A is whitespace, and not captured. B matches either "angle" or "offset", followed by a real number which it captures into $1. C matches "dispersion" followed by a real number which it captures into $2.

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

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.