Forward Pre-check:
1. Positive Pre-check: (? =)
For example:
Foo (? =bar)//Match foo with bar at the back
It can match: Foobar, Foo in Abcfoobar
But can't match: Foo in FOOABC
2. Positive negative pre-check: (?!)
For example:
Foo (?! BAR)//Match foo with no bar behind
It can match: foo123
But does not match: Foobar
Negative to pre-check
1. Negative positive pre-check (? <=)
For example:
(? <=foo) bar//Match bar with Foo above
It can match: Foobar, bar in 123foobar
Can not match: bar in 123bar
2. Negative negative pre-check (? <!)
For example:
(? <!foo) bar//Match bar with no Foo in front
It can match: bar in 123bar
Cannot be matched: Foobar, bar in 123foobar
pre-check can also be nested &NBSP;
For example:
(? <= (? <!foo) bar) Baz //matches a Baz, which is preceded by a bar, But the front of bar can not be foo
(? <=\d{3} ... (? <!999)) Foo //matches a foo, which is preceded by three non-999 characters, three characters before three digits
It can match 123123foo, 111cdefoo, 111c2cfoo inside of FOO
Note: All pre-checks are non-fetch matches and do not consume characters. That is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check.
Reprint: http://3721myth.blog.163.com/blog/static/31831706201132111234121/
Positive pre-checking and reverse pre-checking of regular expressions