正则表达式:检查`abc`然后检查`def`而不用`123`

时间:2014-05-02 19:16:31

标签: regex

如何使用Regex查找以abc开头的所有字符串,以def结尾,但中间不包含123

1 个答案:

答案 0 :(得分:7)

您可以在此处使用否定前瞻

^abc(?:(?!123).)*def$

正则表达式

^              # the beginning of the string
abc            # 'abc'
(?:            # group, but do not capture (0 or more times)
 (?!           # look ahead to see if there is not:
  123          # '123'
 )             # end of look-ahead
 .             # any character except \n
)*             # end of grouping
 def           # 'def'
$              # before an optional \n, and the end of the string