当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Nagative Lookahead

发表于: 2014-06-03   作者:alleni123   来源:转载   浏览次数:
摘要: 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:
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

Nagative Lookahead

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
正则表达式的先行断言和后行断言一共有4种形式: (?=pattern) 零宽正向先行断言(zero-width positive
正则表达式的先行断言和后行断言一共有4种形式: (?=pattern) 零宽正向先行断言(zero-width positive
正则表达式的先行断言和后行断言一共有4种形式: (?=pattern) 零宽正向先行断言(zero-width positiv
(转载)正则表达式的先行断言(lookahead)和后行断言(lookbehind) 正则表达式的先行断言和后行断言一
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号