2011-06-09 95 views
15

什么是匹配有图案的话正则表达式:在正则表达式“列表”

号或资本以任何顺序* 3(+可能的“清单”端)

例如,

OP3 
G6H 
ZZAList 
349 
127List 

都是有效的,而

a3G 
P-0List 
HYiList 
def 
YHr 

全部无效。

回答

34

您可以使用正则表达式:

^[A-Z0-9]{3}(?:List)?$ 

说明:

^  : Start anchor 
[A-Z0-9] : Char class to match any one of the uppercase letter or digit 
{3}  : Quantifier for previous sub-regex 
(?:List) : A literal 'List' enclosed in non-capturing paranthesis 
?  : To make the 'List' optional 
$  : End anchor 

See it

+3

一个可行的治疗,并感谢包括解释过,正则表达式的开始更有意义现在给我。虽然不是太多...; P – w69rdy 2011-06-09 08:52:50

+0

@ w69rdy阅读此。 http://www.slideshare.net/andreizm/andreis-regex-clinic – JJS 2015-05-25 12:55:45