in reply to Re: Leaking Regex Captures
in thread Leaking Regex Captures
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Leaking Regex Captures
by Marshall (Canon) on Aug 05, 2009 at 16:07 UTC | |
by SuicideJunkie (Vicar) on Aug 05, 2009 at 17:02 UTC | |
by Marshall (Canon) on Aug 06, 2009 at 22:29 UTC |