正则表达式匹配字符串

时间:2012-08-02 12:00:40

标签: regex

我希望按照规则匹配所有令人满意的字符串 -

  • 应包含小写字母,数字和短划线
  • 应以字母或数字开头
  • 应以字母或数字结尾
  • 总字符串长度应至少为3,最多为20个字符
  • .是可选的,不应有两个或多个连续点.
  • 短划线-是可选的,不应有两个或更多连续短划线-
  • .和短划线-不应该是连续的//字符串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

任何人都可以帮我纠正这个正则表达式吗?

2 个答案:

答案 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