What are negated character classes that are used in Python regular expressions?

PythonServer Side ProgrammingProgramming

We come across negated character classes in Python regular expressions. 

An regex of ‘[abdfgh]’ matches any single character which is one of ‘a’, ‘b’, ‘d’, ’f’, ’g’ or ‘h’. This is termed a character class.

An regex of ‘[^abdfgh]’ will match any single character which is NOT one of ‘a’, ‘b’, ‘d’, ’f’, ’g’ or ‘h’. This is a negated character class, and is indicated by the ‘^’ character at the start of the character class.

The character ‘^’ has a special meaning at the start of the character class. if it is used elsewhere in that character class it simply means a ‘^’ character.

Negated character classes work with any of the character classes :

The ranges [0–9] match a single digit character, [^0–9] matches any single character that isn’t a digit. 

raja
Published on 09-Jan-2018 17:53:17
Advertisements