How to Match a Number with a Certain Amount of Digits in Python using Regular Expressions 


               


How to Match a Number with a Certain Amount of Digits in Python using Regular Expressions



Python


In this article, we show how to match a number with a certain amount of digits in Python using regular expression.

Say, we want to match the year in a string. A year is a 4-digit number, such as 2016 or 2017.

With Python, we can single out digits of certain lengths. We extract only 4-digit numbers from a string. We can extract only 1-digit numbers or 2-digits numbers or 3-digit numbers or 4-digit numbers or 5-digits numbers, etc.

We do this through the regular expression, regex= "\d{4}"

First, we specify that we want the number to be a digit by, \d

We then, within squiggly braces, to the right of this, put how many digits we want the number to be.

Since we're looking to extract a year, which most of the times is represented as a 4-digit number, we have the statement, regex= "\d{4}"

Let's see how this works in the example below.



So let's now go over this code.

re is the module in Python that allows us to use regular expressions. So we first have to import re in our code, in order to use regular expressions.

After this, we have a variable, phrase, which contains the string that we want to search using regular expressions. You can see that this string has a variety of numbers in it.

We then have a variable, regex, which is set equal to, "\d{4}"

This regular expressions finds digits that are 4 digits in length.

We then use the re.findall() function to find all matches within the string.

This returns ['1991'], which you can see is a list.

If you want to return the result as a string, then you should do a for loop and then the result will be returned as a string.

So now we've gone over how to search for a digit of a certain length in Python using regular expressions.

But what if we don't want a digit that is entirely a fixed length?

What if we want to look for a digit between 4 and 6 digits for instance>

We would do this with the regular expression, regex= "\d{4,6}"

So now any number that is 4-6 digits long will be returned.

So, with the following code below, you see this.



So you can see, 25, 1, and 300 are discarded, because they are less than 4 digits. 1991, 14000, and 200000 are between 4 and 6 digits. Thus, they are included.

And this is how you can match a number with a certain amount of digits in Python using regular expressions.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...