How to do negative pattern matching using regular expressions in Python

A negative pattern in a regular expression returns matches that are not equal to a specified pattern. For example, a negative reverse search for “bcd” in “a bcd” results in “a”.

Solution for How to do negative pattern matching using regular expressions in Python : You can use re.search() to do negative pattern matching Create a regular expression using the syntax ^(?!pattern). where pattern is the pattern for negative pattern matching. Call re.search(pattern, string) with pattern as the result of the previous step to group the occurrences of where pattern is not in string. Call match.group() with match as the previous result to return the strings matching the negative pattern from it.


how-to-do-negative-pattern-matching-using-regular-expressions-in-python