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"(?

Hi
I am a few months new into python. I have used regexps before in perl
and java but am a little confused with this problem.

I want to parse a number of strings and extract only those that
contain a 4 digit number anywhere inside a string

However the regexp
p = re.compile(r'\d{4}')

Matches even sentences that have longer than 4 numbers inside
strings ..for example it matches "I have 3324234 and more"

I am very confused. Shouldnt the \d{4,} match exactly four digit
numbers so a 5 digit number sentence should not be matched .

Here is my test program output and the test given below
Thanks for your help
Harijay

PyMate r8111 running Python 2.5.1 (/usr/bin/python)

>>testdigit.py

Matched I have 2004 rupees
Matched I have 3324234 and more
Matched As 3233
Matched 2323423414 is good
Matched 4444 dc sav 2412441 asdf
SKIPPED random1341also and also
SKIPPED
SKIPPED 13
Matched a 1331 saves
SKIPPED and and as dad
SKIPPED A has 13123123
SKIPPED A 13123
SKIPPED 123 adn
Matched 1312 times I have told you
DONE

#!/usr/bin/python
import re
x = [" I have 2004 rupees "," I have 3324234 and more" , " As 3233 " ,
"2323423414 is good","4444 dc sav 2412441 asdf " , "random1341also and
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"

Python regex find 4 digit number

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