`

Nagative Lookahead

阅读更多
http://stackoverflow.com/questions/1749437/regular-expression-negative-lookahead

A negative lookahead says, at this position, the following regex can not match.

Let's take a simplified example:

a(?!b(?!c))

a      Match: (?!b) succeeds
ac     Match: (?!b) succeeds
ab     No match: (?!b(?!c)) fails
abe    No match: (?!b(?!c)) fails
abc    Match: (?!b(?!c)) succeeds
The last example is a double negation: it allows a b followed by c. The nested negative lookahead becomes a positive lookahead: the c should be present.

In each example, only the a is matched. The lookahead is only a condition, and does not add to the matched text.



如果写了这样的正则表达式  a(?!b)
就表示匹配所有后面没有b紧跟着的a.

也就是说"ab aa ac ad"
就会匹配到
(a)a
a(a)
(a)c
(a)d

这里只有ab的a是匹配不到的, 因为后面紧跟着b
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics