
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Negated Character Classes 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 except '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 eliminate 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'. As a result, it matches and prints all the remaining characters.
import re text = "abcdefghi123" matches = re.findall(r'[^abdfgh]', text) print(matches)
Following is the output of the above code -
['c', 'e', 'i', '1', '2', '3']
Negated Character Classes with Word Boundaries
Another common method is using negated classes to find characters that are not part of certain words or patterns. 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 want to 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)
Following is the output of the above code -
[',', ' ', '!', ' ', ' ', '1', '2', '3']
Excluding Special Characters
We can also exclude special characters by using a negated character class of all special characters.
Example
Here, the regex [^@!#$%] excludes these five special characters. As a result, the program prints all remaining characters.
import re text = "Hello@World! This#is$Python%" matches = re.findall(r'[^@!#$%]', text) print(''.join(matches))
Following is the output of the above code -
HelloWorld ThisisPython