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


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. 

Updated on: 30-Jul-2019

823 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements