Mark As Completed Discussion

Lookahead and Lookbehind

Lookahead and lookbehind are special constructs in regular expressions that allow you to specify a pattern to match based on what comes before or after the current position in the input text.

Positive Lookahead

Positive lookahead is denoted by (?=pattern) and is used to match a pattern only if it is followed by another pattern. For example, the regex (?=regex)python will match the word 'python' only if it is followed by the word 'regex'.

Negative Lookahead

Negative lookahead is denoted by (?!pattern) and is used to match a pattern only if it is not followed by another pattern. For example, the regex (?!regex)python will match the word 'python' only if it is not followed by the word 'regex'.

Positive Lookbehind

Positive lookbehind is denoted by (?<=pattern) and is used to match a pattern only if it is preceded by another pattern. For example, the regex (?<=python) regex will match the word 'regex' only if it is preceded by the word 'python'.

Negative Lookbehind

Negative lookbehind is denoted by (?<!pattern) and is used to match a pattern only if it is not preceded by another pattern. For example, the regex (?<!python) regex will match the word 'regex' only if it is not preceded by the word 'python'.

PYTHON
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment