Regex 用于在长度为8的字母数字字符串之间允许一个连字符的正则表达式
已尝试此正则表达式,但不起作用:Regex 用于在长度为8的字母数字字符串之间允许一个连字符的正则表达式,regex,validation,Regex,Validation,已尝试此正则表达式,但不起作用: ^([a-zA-Z0-9])+([a-zA-Z0-9]-\){8}$ 符合以下条件的正则表达式: 112-4324 1d5g-5HU ER9O5-11 但不匹配: 112-234213421 (more than 8 chars) 1244-53 (less than 8 chars) -23432BB (hyphen at begining) 5tT569K- (hyphen in the end
^([a-zA-Z0-9])+([a-zA-Z0-9]-\){8}$
符合以下条件的正则表达式:
112-4324
1d5g-5HU
ER9O5-11
但不匹配:
112-234213421 (more than 8 chars)
1244-53 (less than 8 chars)
-23432BB (hyphen at begining)
5tT569K- (hyphen in the end)
234-23-5 (two hyphens)
RTG--43T (two consecutive hyphens)
您可以基于前瞻性使用此正则表达式:
^(?=[a-zA-Z0-9-]{8}$)[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)$
为正向前瞻,以确保输入中有8个字符(?=[a-zA-Z0-9-]{8}$)
(?=
开始一个积极的前瞻。如果我想让234-23-5也验证为真,该怎么办。这个正则表达式将正常工作^(?=[a-zA-Z0-9-]{8}$)[a-zA-Z0-9]+(?:-[a-zA-Z0-9-]+$
。是的,这几乎是正确的。^(?=[a-zA-Z0-9-]{8}$)[a-zA-Z0-9](?:-++-zA+-9]>/code>。