我要求根据以下规则验证人员名字缩写:
这是我所拥有的:
^[[:alpha:]]([[:alpha:]]|([[:blank:]-])[[:alpha:]])*([[:blank:]-][:alpha:]|[:alpha:])?$
它执行1和2(并允许空格和连字符),但不能确保字母字符由空格或连字符分隔,即AD计算结果为真。
感谢您的帮助。我是正则表达式的新手,所以任何帮助都表示赞赏。
答案 0 :(得分:0)
^([A-Z][ -])*[A-Z]$
- start
- -- any number of times
----- a capital letter
---- followed by exactly one separator (space or hyphen)
----- followed by a final (and possibly initial) capital letter
答案 1 :(得分:0)
Alpha符号或A-Z?
[:alpha:] == [A-Za-z]
试试这个RE:
^[A-Z]([\s\-][A-Z])*$
"A B C" = true
"ABC" = false
"123" = false
"A B-C" = true
"A BC" = false
RE是否允许单个字母字符或多个字符?