Python Regular Expression Match All 5 Digit Numbers but None Larger


I'm attempting to string match 5-digit coupon codes spread throughout a HTML web page. For example, 53232, 21032, 40021 etc... I can handle the simpler case of any string of 5 digits with [0-9]{5}, though this also matches 6, 7, 8... n digit numbers. Can someone please suggest how I would modify this regular expression to match only 5 digit numbers?

8/20/2010 4:40:26 PM

Accepted Answer

>>> import re
>>> s="four digits 1234 five digits 56789 six digits 012345"
>>> re.findall(r"\D(\d{5})\D", s)

if they can occur at the very beginning or the very end, it's easier to pad the string than mess with special cases

>>> re.findall(r"\D(\d{5})\D", " "+s+" ")
8/20/2010 4:54:02 PM

full string: ^[0-9]{5}$

within a string: [^0-9][0-9]{5}[^0-9]

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow