How to Extract Non-Digits from a String in Python Using Regular Expressions 


               


How to Extract Non-Digits from a String in Python Using Regular Expressions



Python


In this article, we show how to extract non-digits from a string in Python using regular expressions.

So, say, we have the string, "334 animals"

In certain cases in code, we may want to extract just the non-digit characters from the string.

In Python, with regular expressions, we can do this easily.

We simply write a regular expression that matches non-digits. Any non-digits matched in the string will be returned as output.

The regular expression statement that matches non-digits is shown below.



This regular expression above will match all non-digits found in the string and return only the non-digits (and not the digit characters).

To get the full picture, let's look at a complete example.

This is shown in the code below.



So the first thing is that in order to use regular expressions in Python, you have to import the re module. So this is the first thing we do in our code above.

Next, we have the phrase that we extract from, "334 animals"

Our goal is to write a regular expression that gets the non-digit characters from the string.

We then have our regular expression which we assign to the variable, patterns.

Next, when you're using a regular expression to match a pattern in a string, you must use a for loop for the pattern that you create.

The reason for this is that patterns checks multiple instances of the string. Therefore, it is not just checking the entire string, "334 animals" just all as a whole. It is checking every instance of the string to see if there are multiple areas in the string that matches this pattern. This is more easily understandable when you have multiple non-digit characters separated in a string. In the case of having multiple non-digit characters that are separate in the string, it will return each non-digit group separately rather than as a clump, which is what is more than likely what is intended.

We then create a variable called match and set it equal to, re.findall(p, phrase)

With this line, we are looking to see if any of the phrase has non digits.

If so, Python returns it as a list.

We then print out the result.

The result we get is shown below.



We can also create the code with a function, so that we can just call the function and return the results.

This is shown below.



The code above returns the same value of 3.

Multiple Non-digits in a String

Lastly, let's look at an example, where there are multiple nondigit groups separated in a string.

Let's look at what Python returns when we extract the non-digits from a string with multiple nondigits that are separate from each other.



This is the same exact code as above, but just a different string that contains multiple nondigits that are separated from each other, this time.

The code, above, returns the following output.



So you see that Python just returns the non-digits as a list.

So this is extracting non-digits from a string in Python using regular expressions.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...