更简洁的regex匹配字符串可选部分的方式

时间:2021-06-10 09:39:36

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.

我正在编写与一系列命令匹配的Flex lexer模式。与subversion的命令行客户端相似,命令可以缩短为一个很小但仍然明确的长度。

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?

它运行得很好,但是闻起来很糟糕尤其是在很长的定义上,有更短的方法来定义这样的匹配吗?

2 个解决方案

#1


2  

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

如果你可以使用象$或\b这样的令牌结尾符号,你可以:

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

#2


1  

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

关于我的什么? C(O(M(M((ND)?)?)?)吗?)?这是唯一的另一种写法。

#1


2  

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

如果你可以使用象$或\b这样的令牌结尾符号,你可以:

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

#2


1  

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

关于我的什么? C(O(M(M((ND)?)?)?)吗?)?这是唯一的另一种写法。