How to match any non-digit character in Python using Regular Expression?

A regular expression is a group of characters that allows you to use a search pattern to find a string or a set of strings. RegEx is another name for regular expressions. The re module in Python is used to work with regular expressions.

In this article, we learn how to extract non-digit characters in Python using regular expressions. We use \D+ regular expression in Python to get non-digit characters from a string.

Where,

  • \D returns a match that does not contain digits.
  • + implies one or more occurrences of characters.

Using findall() function

The re.findall() function in Python matches the specified pattern with the given string and returns all the non-overlapping matches, as a list of strings or tuples.

Example 1: Basic Non-Digit Matching

In the following example code, we use the findall() function to match any non-digit character in Python using a regular expression ?

import re

string = "2018Tutorials point"
pattern = r'\D+'
match = re.findall(pattern, string)
print(match)

The output of the above code is ?

['Tutorials point']

Example 2: String with Multiple Digits

Let us see another example where a string has multiple digits. Here, we assume '5 children, 3 boys, 2 girls' as an input phrase. The output should return all the strings with non-digits ?

import re

string = "5 children 3 boys 2 girls"
pattern = r'\D+'
match = re.findall(pattern, string)
print(match)

The output of the above code is ?

[' children ', ' boys ', ' girls']

Using search() function

The re.search() function searches the string for a match and returns a match object if there is a match. The group() method is used to return the part of the string that is matched.

Example

In the following example code, we use search() function to find a specific pattern and then remove digits from the matched text ?

import re

phrase = 'RANDOM 5children 3 boys 2 girls//'
pattern = r'(?<=RANDOM).*?(?=//)'
match = re.search(pattern, phrase)
text = match.group(0)
nonDigit = re.sub(r'\d', '', text)
print(nonDigit)

The output of the above code is ?

 children  boys  girls

Using sub() function to Remove Digits

The re.sub() function can be used to substitute digits with empty strings, effectively keeping only non-digit characters ?

import re

string = "Hello123World456"
result = re.sub(r'\d', '', string)
print(result)

The output of the above code is ?

HelloWorld

Comparison of Methods

Method Returns Best For
findall() List of all matches Finding all non-digit sequences
search() First match object Finding first occurrence
sub() Modified string Removing digits completely

Conclusion

Use \D+ pattern with findall() to extract all non-digit character sequences. Use sub() to remove digits and keep only non-digit characters. Choose the appropriate method based on whether you need to find, extract, or replace patterns.

Updated on: 2026-03-24T18:56:09+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements