如果第一个字符是数字,那么第二个字符应该是字符,其余的应该是数字、字母和连字符的任何出现。
我尝试了:
([A-Za-z-]{1})(?![A-Za-z-]{1})([A-Za-z-]{61})
字符串有效:
例如:
1 : invalid
11 : invalid
;1 : invalid
1; : invalid
a; : invalid
;a : invalid
- : valid
a : valid
aa : valid
a1 : valid
1a : valid
1- : valid
-1 : valid
a- : valid
-a : valid
11testisgoingon : invalid
;1testingisgoingon : invalid
1;testingisgoingon : invalid
a;testingisgoingon : invalid
;atestingisgoingon : invalid
-testingisgoingon : valid
atestingisgoingon : valid
aatestingisgoingon : valid
a1testingisgoingon : valid
1atestingisgoingon : valid
1-testingisgoingon : valid
-1testingisgoingon : valid
a-testingisgoingon : valid
-atestingisgoingon : valid
([A-Za-z-])(?![A-Za-z-])
但是这对前两个字符很有效。但是如果有两个以上的字符,那么它是不正确的。
试试看
^(([0-9][a-z-][a-z0-9-]{0,60})?(-?[a-z0-9])?([a-z0-9]-?)?([a-z-][a-z0-9-]{0,62})?){1}$
带有标志“多行,IgnoreCase”
11testisgoingoninvalid
;1testingisgoingonnvalid
1;testingisgoingoninvalid
a;testingisgoingoninvalid
;atestingisgoingoninvalid
-testingisgoingonvalid
atestingisgoingonvalid
aatestingisgoingonvalid
a1testingisgoingonvalid
1atestingisgoingonvalid
1-testingisgoingonvalid
-1testingisgoingonvalid
a-testingisgoingonvalid
-atestingisgoingon
在https://regexr.com/.
解释:
将IgnoreCase标志添加到正则表达式或将A-Z
添加到任何[a-z]
^((...)?(...)?(...)?(...)?){1}$
-> We set up an outer capturing group containing
4 inner ones wich may or may no occure. In total
only 1 must match.
The capturing group must fill the whole line, and
the inner caputring groups model your requirements:
([0-9][a-z-][a-z0-9-]{0,60})?
-> STARTS with numeric,
followed by alphabetic or hyphen,
followed by anything
up to 63 characters in total
(-?[a-z0-9])?
([a-z0-9]-?)?
-> one character prefixed/followed by hyphen
([a-z-][a-z0-9-]{0,62})?
-> generic long text not started by numeric
followed by anything
up to 63 characters long