2011-02-04 84 views

回答

11
^(?!-)(?!.*--)[A-Za-z0-9-]+(?<!-)$ 

说明:

^    # Anchor at start of string 
(?!-)   # Assert that the first character isn't a - 
(?!.*--)  # Assert that there are no -- present anywhere 
[A-Za-z0-9-]+ # Match one or more allowed characters 
(?<!-)  # Assert that the last one isn't a - 
$    # Anchor at end of string 
22

^[A-Za-z0-9]+(-[A-Za-z0-9]+)*$

使用该正则表达式,连字符才刚刚组内匹配。这个连字符的每一边都有[A-Za-z0-9]+子表达式。由于此子表达式与一个或多个字母数字字符相匹配,因此连字符不可能匹配另一个连字符的开始,结尾或旁边。

+0

这很聪明!可能比我所有的视角更容易理解。 – 2011-02-04 11:21:25

1
^[a-zA-Z0-9](?!.*--)[a-zA-Z0-9-]*[a-zA-Z0-9]$ 

^[a-zA-Z0-9]  /*Starts with a letter or a number*/ 
(?!.*--)   /*Doesn't include 2 dashes in a row*/ 
[a-zA-Z0-9-]* /*After first character, allow letters or numbers or dashes*/ 
[a-zA-Z0-9]$  /*Ends with a letter or a number*/ 

匹配:

重新播放/ 重新上场-ED

不MATC H:

Replay-/RE - 播放/ -replay

0

如果“ - ”不是在字符串的开始也没有结束允许,您正在寻找“一个或多个alanum的序列,接着是一个短划线的一个或多个基团(S),随后1个或多个alanum”

/[0-9A-Z]+(-[0-9A-Z]+)+/ 

简单与正则表达式的宝贵格言。 (nota:搜索小写字母,添加它们,我没有为了清晰起见)

相关问题