Note that this is very closely related to the context of: Re: Regex - Matching prefixes of a word

The original goal of the regex is to match a command string similar to:
beam 15 crew 5 wounded 2 critical to S.S.Kevorkian
Where the number-type pairs are optional and may appear in any order, provided that there is at least one of the pairs present. (No point in beaming nobody over)

Thus, the (\d+)\s*literals form of each piece,
and the (?: (capture)X | (capture)Y | (capture)Z )+ overall structure.
Wrapped around that structure is a /^(?:$regexSubstringOf{beam}|$regexSubstringOf{transport}\s* )\s*(?:$structure)\s+(?:to\s+)?$regexObjectName\s*$/i

And then it all ends up in an addCommand('transport', {crew=>$1,wound=>$2,crit=>$3},$4) if $cmd =~ /regex/i; ($4 is the ship name, captured by the $regexObjectName)


What I have done to work around the problem is to capture the whole pair, and then inside the addCommand() function, I fire off some more regex to s/\D//g the hash values if they are defined.
I also have to add a negative lookahead in the captures to prevent '5 crit' from matching as a substring of 'crew': "5cr" and stomping the $1 value before backtracking kicks in.



To sum up; I want the numbers out of those pairs, with $1 = Number of healthy Crew, $2 = number of wounded, $3 = number of critically injured.
How I get them is not important, and for multiple copies of them in the command string I don't care which one gets picked, although consistency is desirable and the last one is better than the first since that means a user can just keep typing if they make a mistake, instead of backspacing up to change the number.


In reply to Re^2: Leaking Regex Captures by SuicideJunkie
in thread Leaking Regex Captures by SuicideJunkie

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.