Nick Veys
Nick Veys

Reputation: 23939

More concise way of regex matching optional portions of a string

I'm writing Flex lexer patterns to match a series of commands. Not unlike subversion's command line client, the commands can be shortened to a small but still unambiguous length.

So a command such as:

MYCOMMAND

Could be entered as either:

MYCOMMAND
MCOMMAND
MYCOM
MC

The pattern I have been ignorantly writing for these cases looks like:

M(Y)?C(O|OM|OMM|OMMA|OMMAN|OMMAND)?

And it works fine, but it smells pretty bad especially on very long definitions, is there a shorter way of defining such a match?

Upvotes: 2

Views: 299

Answers (2)

John Kugelman
John Kugelman

Reputation: 361605

If you can use an end-of-token symbol like $ or \b then you could do:

MY?C(O|$)(M|$)(M|$)(A|$)(N|$)(D|$)

Upvotes: 2

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

What about MY?C(O(M(M(A(ND?)?)?)?)?)? ;-)
IMHO, this is the only other way to write it.

Upvotes: 1

Related Questions