regex-howto
lookahead / lookbehind
conclusion
A(?=B) # positive lookahead
A(?!B) # negative lookahead
(?<=A)B # positive lookbehind
(?<!A)B # negative lookbehind
So, the =
means positive, and the !
means negative, the ?
means look, the <
means back.
ref:
detail
I want to match the url in the image tag, but except those online ones.
There are quite a lot ways to help me finish this.
For example, I first matched an img, and extract the url, then test if the url starts with http
.
But it's a bit slow, and needs a little more codes. (i.e. not atomic)
I also came up with an 折中的 idea, i.e. just to match those start with '.', which indicates a local url.
However, I want a step further.
I know then it's so-called lookahead or lookbehind.
So I did a quite hard search, and finally I derived my answer from here:
And then I am surprised to find what I wanna to realize can be easily finished via look-ahead
based on src="
rather than look-behind
based on a ANYTHING
.
How 巧妙的 this is!
core ref:
Thanks for YuanHao
regex - RegExp matching string not starting with my - Stack Overflow
pcre - RegEx for combining "match everything" and "negative lookahead" - Stack Overflow
ref:
it seems ok but indeed not.
A detailed tutorial of a professional regex website