我希望按照规则匹配所有令人满意的字符串 -
.
是可选的,不应有两个或多个连续点.
-
是可选的,不应有两个或更多连续短划线-
.
和短划线-
不应该是连续的//字符串aaa.-aaabbb
无效我想出了这个正则表达式:
^[a-z0-9]([a-z0-9]+\.?\-?[a-z0-9]+){1,18}[a-z0-9]$
[a-z0-9] //should start/end with a letter or a number
([a-z0-9]+\.?\-?[a-z0-9]+){1,18} //other rules
然而,在某些情况下失败,例如 -
abcdefghijklmnopqrstuvwxyz //should fail total number of chars greater than 20
aaa.-aaabbb //should fail as dot '.' and dash '-' are consecutive
任何人都可以帮我纠正这个正则表达式吗?
答案 0 :(得分:3)
您可以使用lookahead assertion:
来实现此目的^(?!.*[.-]{2})[a-z0-9][a-z0-9.-]{1,18}[a-z0-9]$
<强>解释强>
^ # Start of string
(?! # Assert that the following can't be matched:
.* # Any number of characters
[.-]{2} # followed by .. or -- or .- or -.
) # End of lookahead
[a-z0-9] # Match lowercase letter/digit
[a-z0-9.-]{1,18} # Match 1-18 of the allowed characters
[a-z0-9] # Match lowercase letter/digit
$ # End of string
答案 1 :(得分:1)
我提出了这个问题,它使用类似于蒂姆解决方案的negative lookahead,但采用了不同的方法。因为它只是在看到点或短划线时才会向前看,所以可能不需要做太多back tracking,这可能会使其表现得更快。
^[a-z0-9]([a-z0-9]|([-.](?![.-]))){1,18}[a-z0-9]$
说明:
^ # Start of string
[a-z0-9] # Must start with a letter or number
( # Begin Group
[a-z0-9] # Match a letter or number
| # OR
([-.](?![.-])) # Match a dot or dash that is not followed by a dot or dash
){1,18} # Match group 1 to 18 times
[a-z0-9] # Must end with a letter or number
$ # End of string