Most characters and metacharacters cause the matching engine to move forward in some way. Not so for the zero-width assertions. They don’t make the engine advancing through the string, they simply succeed or fail at the current cursor position of the engine.
Simple zero-width assertions
The following table lists the simpler zero-width assertions from the regex language:
|OR operator. It has a very low precedence.
|Matches at the beginning of lines in MULTILINE mode. Otherwise, matches only at the beginning of the string.|
|Matches at the end of lines in MULTILINE mode. Otherwise, matches only at the end of the string (and immediately before the newline – if any – at the end of the string).|
|Matches only at the beginning of the string. When not in MULTILINE mode,
|Matches only at the end of the string.|
|Word boundary: matches at the beginning or end of a word. A word is a sequence of alphanumeric characters. The beginning or end of a word is any whitespace or other non-alphanumeric character.
|Matches if current position is NOT a word boundary.|
Lookahead assertions are especially useful to match filenames. The following example matches all filenames, except
Lookbehind has the same effect, but works backwards. It tells the regex engine to temporarily step backwards in the string and check if the lookbehind token can be matched there.