Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking if year is in the string (4 consecutive digits)

Tags:

python

list

match

How can I find if a strings in a list contains a year (ex. 1999). I guess I would check for four consecutive digits such as: [1-2][0-9][0-9][0-9]

How to check that against a list piece? Here is what I've tried already

for piece in reflist:
    if "\d{4}" in piece:
        # Do something

for piece in reflist:
    if re.match('\d{4}', piece):
        print piece + '\n'
like image 571
raw-bin hood Avatar asked Apr 14 '13 13:04

raw-bin hood


2 Answers

You want to use re.search() to test for matches anywhere in the input string.

To match (recent) years a little more precisely, you could use:

re.search(r'[12]\d{3}', piece)

which would match anything from 1000 through to 2999.

like image 85
Martijn Pieters Avatar answered Nov 14 '22 22:11

Martijn Pieters


While both '\d{4}' and r'[12]\d{3}' will return 4 consecutive digits, they will also return the first 4 digits of a larger number like 199999.

To get an occurrence of a year like the OP example of 1999, wrap the expression with \s which will match for whitespace characters.

r'\s[12]\d{3}\s'
like image 36
pieguy Avatar answered Nov 14 '22 23:11

pieguy