Python regex find 4 digit number
@ExplosionPills is correct, but there would still be two problems with your regex. First,
The added portion is a negative lookahead assertion, meaning, "...not followed by any more numbers." Let me simplify that by the use of
That's better. Now, the second problem. You have no constraint on the left side of your regex, so given a string like Hi I want to parse a number of strings and extract only those that However the regexp Matches even sentences that have longer than 4 numbers inside I am very
confused. Shouldnt the \d{4,} match exactly four digit Here is my test program output and the test given below PyMate r8111 running Python 2.5.1 (/usr/bin/python) >>testdigit.py Matched I have 2004 rupees #!/usr/bin/python p = re.compile(r'\d{4} ') for elem in x: 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 |