matching exactly a 4 digit number in python

Mr.SpOOn mr.spoon21 at
Fri Nov 21 23:07:10 CET 2008

2008/11/21 harijay <harijay at>:
> 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"

Try with this:

p = re.compile(r'\d{4}$')

The $ character  matches the end of the string. It should work.

More information about the Python-list mailing list