这是一个正常的寄存器名,可以是1-n个字符以a-zA-Z与-,像
larry-cai, larrycai, larry-c-cai, l,
Run Code Online (Sandbox Code Playgroud)
但-不能像第一个和最后一个角色一样
-larry, larry-
Run Code Online (Sandbox Code Playgroud)
我的想法是这样的
^[a-zA-Z]+[a-zA-Z-]*[a-zA-Z]+$
Run Code Online (Sandbox Code Playgroud)
但如果我的正则表达式,长度应为2
应该很简单,但不要怎么做
如果你可以写它并通过http://tools.netshiftmedia.com/regexlibrary/会很好
您没有指定您正在使用的正则表达式引擎.一种方法是(如果您的引擎支持环视):
^(?!-)[A-Za-z-]+(?<!-)$
Run Code Online (Sandbox Code Playgroud)
说明:
^ # Start of string
(?!-) # Assert that the first character isn't a dash
[A-Za-z-]+ # Match one or more "allowed" characters
(?<!-) # Assert that the previous character isn't a dash...
$ # ...at the end of the string.
Run Code Online (Sandbox Code Playgroud)
如果lookbehind不可用(例如在JavaScript中):
^(?!-)[A-Za-z-]*[A-Za-z]$
Run Code Online (Sandbox Code Playgroud)
说明:
^ # Start of string
(?!-) # Assert that the first character isn't a dash
[A-Za-z-]* # Match zero or more "allowed" characters
[A-Za-z] # Match exactly one "allowed" character except dash
$ # End of string
Run Code Online (Sandbox Code Playgroud)