in reply to Re: Regex - Matching prefixes of a word
in thread Regex - Matching prefixes of a word

I do not actually have any nesting in the command structure. The code in the OP was just a simple way to test suggestions.

The code actually used in the game is very easy to chain together:

# Fire! $cmd =~ /^$regexSubstringOf{fire}\s+$regexName$regexIndexes\s*$/i ? se +tCommand($player, 'fire',[] , $1, $2) : # Lock weapons on a target $cmd =~ /^$regexSubstringOf{lock}\s+$regexName$regexIndexes\s+(?:on\s+ +)?$regexName$regexLockParams?\s*$/i ? setCommand($player, 'aim',[$4,$ +5,$3] , $1, $2) : ...
Adding new ways to say a command is as easy as copy-pasting one of those lines. Adding new commands is slightly more complicated, as it requires adding backend code to the addCommand() function too.

I had briefly considered a parser at first. I have already got one from another project that can evaluate math formulas, save variables and even do a form of looping. It didn't seem like a good idea at the time, because the token overlap (lock on dock, dock with lock) seemed like trouble, I expected I'd have to put at least as much effort into deciding based on tokens as on the regex captures, and finally, the direct regex way seemed pretty simple and straightforward.