Match Non-Digit Characters 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 zero 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

In the following example code, we use the findAll() function to match any non-digit character in Python using a regular expression. We begin by importing the regular expression module. Then, we have used findall() function, which is imported from the re module.

import re
string = "2018Tutorials point"
pattern= [r'\D+']
for i in pattern:
   match= re.findall(i, string)
   print(match)

After running the above program, the following output is obtained ?

['Tutorials point']

Example

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+']
for i in pattern:
   match= re.findall(i, string)
   print(match)

Following is the output of the above program -

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

Using search() function

This re.search() function searches the string/paragraph 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 match any non-digit character in Python using a regular expression. We begin by importing the regular expression module. Then, we have used search() function, which is imported from the re module, to get the required string. 

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)

Following is the output of the above program -

children  boys  girls
Updated on: 2025-04-23T15:55:15+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements