Python regex find 4 digit number

@ExplosionPills is correct, but there would still be two problems with your regex.

First, $ matches the end of the string. I'm guessing you'd like to be able to extract an int in the middle of the string as well, e.g. abcd123456efg789 to return 123456. To fix that, you want this:

r"[0-9]{4,7}[?![0-9]]"
            ^^^^^^^^^

The added portion is a negative lookahead assertion, meaning, "...not followed by any more numbers." Let me simplify that by the use of \d though:

r"\d{4,7}[?!\d]"

That's better. Now, the second problem. You have no constraint on the left side of your regex, so given a string like abcd123efg123456789, you'd actually match 3456789. So, you need a negative lookbehind assertion as well:

r"[? also" ,"","13"," a 1331 saves" ,"
and and as dad"," A has 13123123","
A 13123","123 adn","1312 times I have told you"]

p = re.compile[r'\d{4} ']

for elem in x:
if re.search[p,elem]:
print "Matched " + elem
else:
print "SKIPPED " + elem

print "DONE"

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

Chủ Đề