正则的进阶表达——找出(不)包含特定字符串

主要运用正则语法中的(积极/消极)前瞻、后顾方法,以匹配或排除特定的字符串。

可简洁明了的如下表达,以便于理解:

(?<!behind)regexp(?=ahead)

相关语法介绍:

(积极)前瞻:

(?=ahead)

Asserts that the given subpattern can be matched here, without consuming characters
/foo(?=bar)/

foobar foobaz

 
 

(消极)后顾:

(?<!behind)
(?<!...)
Ensures that the given pattern would not match and end at the current position in the expression. The pattern must have a fixed width. Does not consume characters.
/(?<!not )foo/

not foo but foo

 

猜你喜欢

转载自www.cnblogs.com/timace/p/10261378.html