Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are negated character classes that are used in Python regular expressions?
While working with Python regex, if we want to match everything except certain characters, then we can use negated character classes by placing a caret (^) as the first character inside square brackets. The pattern [^abdfgh] will match any character not in that set.
What is a Negated Character Class?
A character class like [abc] matches any single character that is 'a', 'b', or 'c'. But if we use a ^ symbol at the beginning, like [^abc], it will match any character except 'a', 'b', or 'c'. This allows us to exclude certain characters from the match quickly.
The position of the ^ is important ? it must come right after the opening square bracket to indicate negation. If we place it somewhere else, it will be treated as a literal character.
Basic Negation of Specific Characters
Let's see how to exclude specific alphabets from matching using a negated character class and re.findall() method to find all occurrences in the given string ?
Example
In the following program, we want to find all characters in a string that are not in the group. The regex [^abdfgh] matches any character that is not 'a', 'b', 'd', 'f', 'g', or 'h' ?
import re text = "abcdefghi123" matches = re.findall(r'[^abdfgh]', text) print(matches)
The output of the above code is ?
['c', 'e', 'i', '1', '2', '3']
Negated Character Classes with Character Ranges
We can use negated classes to find characters that are not part of certain ranges. For instance, the regex [^a-zA-Z] will locate all characters that aren't letters, such as spaces, punctuation, or numbers ?
Example
In the following, we extract all non-alphabetic characters from a string using negated character classes ?
import re text = "Hello, World! 123" matches = re.findall(r'[^a-zA-Z]', text) print(matches)
The output of the above code is ?
[',', ' ', '!', ' ', '1', '2', '3']
Excluding Special Characters
We can also exclude special characters by using a negated character class of specific special characters ?
Example
Here, the regex [^@!#$%] excludes these five special characters. The program prints all remaining characters joined together ?
import re
text = "Hello@World! This#is$Python%"
matches = re.findall(r'[^@!#$%]', text)
print(''.join(matches))
The output of the above code is ?
HelloWorld ThisisPython
Excluding Digits
A common use case is excluding all digits from text. The pattern [^0-9] or [^\d] matches any character that is not a digit ?
Example
import re
text = "Price: $25.99 for item #42"
matches = re.findall(r'[^0-9]', text)
print(''.join(matches))
The output of the above code is ?
Price: $. for item #
Conclusion
Negated character classes using [^...] provide a powerful way to match characters by exclusion rather than inclusion. They are particularly useful for filtering out unwanted characters or extracting everything except specific patterns.
