Sign in Start free

14Lookbehind (?<= ) and (?<! )

Check what comes before the current position.

Lookbehind looks to the left: (?<=\$)\d+ matches the digits right after a dollar sign, and the dollar sign is not part of the match. (?<!-)\b\d+ matches numbers that are not preceded by a minus sign.

JavaScript allows any pattern inside a lookbehind. Java needs a lookbehind with a bounded maximum length (so (?<=a+) is an error in Java but (?<=a{1,10}) is fine).

Exercises