Nagative Lookahead
发布时间:2020-12-14 01:45:15 所属栏目:百科 来源:网络整理
导读: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: (?
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |