Remove characters matching Regex from a String in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Remove characters matching Regex from a String in Python
  2. Removing specific characters from a string with a regex
  3. Removing specific characters with a generator expression

# Remove characters matching Regex from a String in Python

Use the re.sub() method to remove the characters that match a regex from a string.

The re.sub() method will remove the matching characters by replacing them with empty strings.

main.py
import re my_str = '!bobby @hadz #com $abc' result = re.sub(r'[!@#$]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ 'bobby hadz com abc' result = re.sub(r'[^!@#$]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ '!@#$'

remove characters matching regex from string

The code for this article is available on GitHub

The first example uses the re.sub() method to remove the characters that match a regular expression from a string.

The re.sub() method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

main.py
import re my_str = '1bobby, 2hadz, 3com' result = re.sub(r'[0-9]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ 'bobby, hadz, com' result = re.sub(r'[a-zA-Z]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ '1, 2, 3'

If the pattern isn't found, the string is returned as is.

# Removing specific characters from a string with a regex

If you have specific characters you'd like to remove, add them between square brackets.

main.py
import re my_str = '!bobby @hadz #com $abc' result = re.sub(r'[!@#$]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ 'bobby hadz com abc' result = re.sub(r'[^!@#$]', '', my_str) print(result) # ๐Ÿ‘‰๏ธ '!@#$'

remove specific characters from string with regex

The code for this article is available on GitHub

The square brackets [] are used to indicate a set of characters.

The first example removes the specified characters by replacing them with empty strings.

The second example uses the caret ^ symbol.

The caret ^ at the beginning of the set means "NOT". In other words, match all characters that are NOT !, @, # and $.

# Removing specific characters with a generator expression

Alternatively, you can use a generator expression with the str.join() method.

main.py
my_str = '!bobby @hadz #com $abc' characters_to_remove = '!@#$' result = ''.join( char for char in my_str if char not in characters_to_remove ) print(result) # ๐Ÿ‘‰๏ธ 'bobby hadz com abc'

remove specific characters with generator expression

The code for this article is available on GitHub

We used a generator expression to iterate over the string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the character is not one of the characters to be removed and return the result.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise, it evaluates to False.

x not in s returns the negation of x in s.
main.py
print('abc' not in 'bobbyhadz.com') # ๐Ÿ‘‰๏ธ True print('bob' not in 'bobbyhadz.com') # ๐Ÿ‘‰๏ธ False

The last step is to use the str.join() method to join the remaining strings.

main.py
my_str = '!bobby @hadz #com $abc' characters_to_remove = '!@#$' result = ''.join( char for char in my_str if char not in characters_to_remove ) print(result) # ๐Ÿ‘‰๏ธ 'bobby hadz com abc'
The code for this article is available on GitHub

The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

We used an empty string as the separator between the elements because we want to join the remaining characters without a separator.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev