What does ‘(?<!…)’ or negative lookbehind represents in regex (Regular expression)?
(?<!…) is used to checks if any preceding character or pattern does not match the specified pattern.
Syntax: (?<!preceedingPattern)pattern
Let’s see with example:
We have some lines below:
hello world
Hiworld
helloWorld
SeeWorld
From these lines we will fetch world only if its prefix is not hello.
Regex will be:
(?i)(?<!hello)world
Output:
hello world
Hiworld
helloWorld
SeeWorld
Explanation of regex:
(?i): Case-insensitive flag.
(?<!: Represents negative lookbehind. It ensures to match world when hello does not occurs before world.
hello: Represents the word hello which should not be the prefix of world.
): Represents ending of the negative lookbehind.
world: Represents the actual word to match.