Regex | Negative Lookahead

Example

You have a test String S.
Write a regex that can match all characters which are not immediately followed by that same character.
Input: gooooo
Output: Number of matches : 2

Regex_Pattern = r"(.)(?!\1)"

import re

Test_String = input()

match = re.findall(Regex_Pattern, Test_String)

print("Number of matches :", len(match))

Theory

regex_1(?!regex_2)
The negative lookahead (?!) asserts regex_1 not to be immediately followed by regex_2. Lookahead is excluded from the match (do not consume matches of regex_2), but only asserts whether a match is possible or not.


Source: https://yellorn.com/programming/regex-negative-lookahead

Comments