35

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 Answers 8

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

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+" ")
3
  • 2
    I highlighted difference between \D and \b in regex in my answer . Sep 29, 2014 at 18:57
  • re.findall(r"\D(\d{5})\D","15/05/2018 a8711 43160") gives [ ]. What gives?
    – Imad
    Jun 8, 2018 at 10:09
  • 1
    @Aetos. It says right there in the answer that you would need to pad this input string. Lookahead and look behind can greatly reduce the performance of your regex. I would be interested if you could report what you find. Jun 9, 2018 at 21:45
22

Without padding the string for special case start and end of string, as in John La Rooy answer one can use the negatives lookahead and lookbehind to handle both cases with a single regular expression

>>> import re
>>> s = "88888 999999 3333 aaa 12345 hfsjkq 98765"
>>> re.findall(r"(?<!\d)\d{5}(?!\d)", s)
['88888', '12345', '98765']
1
  • 1
    Your answer is correct, the one selected as the solution isn't
    – Imad
    Jun 8, 2018 at 10:11
16

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

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

4
  • thanks, actually, confused with solution '\b\d{5}\b' and '^[0-9]{5}$', thanks for point it out, the later one then create the expression as a string, that is what I need.
    – zhihong
    Mar 24, 2017 at 12:56
  • this answer don't work at the begining and of the string. For example it doesn't match "12345" Mar 28, 2017 at 11:37
  • @Crayon Violent [^0-9][0-9]{5}[^0-9] --> Can you please explain this regex ? Feb 16, 2019 at 14:34
  • @DhiwakarRavikumar [^0-9] Any 1 character that is not a digit, followed by [0-9]{5} exactly 5 digits, followed by [^0-9] any 1 character that is not a digit Feb 20, 2019 at 4:53
6

Note: There is problem in using \D since \D matches any character that is not a digit , instead use \b. \b is important here because it matches the word boundary but only at end or beginning of a word .

import re  

input = "four digits 1234 five digits 56789 six digits 01234,56789,01234"


re.findall(r"\b\d{5}\b", input)  

result : ['56789', '01234', '56789', '01234']

but if one uses re.findall(r"\D(\d{5})\D", s) output : ['56789', '01234'] \D is unable to handle comma or any continuously entered numerals.

\b is important part here it matches the empty string but only at end or beginning of a word .

More documentation: https://docs.python.org/2/library/re.html

More Clarification on usage of \D vs \b:

This example uses \D but it doesn't capture all the five digits number.

This example uses \b while capturing all five digits number.

Cheers

7
  • 1
    I changed your regex to use a raw string there since \b is otherwise a valid escape character in strings. The problem with this answer is that it doesn't work for if other alpha characters are joined to the string of digits eg. "56789a" Sep 29, 2014 at 20:05
  • @gnibbler neither is \D able to handle it too ! Sep 29, 2014 at 20:13
  • You mean \D doesn't handle the beginning or end of strings? I dealt with that in my answer. Sep 29, 2014 at 20:15
  • @gnibbler i mean it doesn't handle "56789a" Sep 29, 2014 at 20:25
  • "56789a" is matched as "56789" using \D Sep 29, 2014 at 20:28
3

A very simple way would be to match all groups of digits, like with r'\d+', and then skip every match that isn't five characters long when you process the results.

1

You probably want to match a non-digit before and after your string of 5 digits, like [^0-9]([0-9]{5})[^0-9]. Then you can capture the inner group (the actual string you want).

1
  • that doesn't work with "12345" as the start of the string and the end of he string are not matched with [^0-9] Mar 28, 2017 at 11:40
1

You could try

\D\d{5}\D

or maybe

\b\d{5}\b

I'm not sure how python treats line-endings and whitespace there though.

I believe ^\d{5}$ would not work for you, as you likely want to get numbers that are somewhere within other text.

1
  • the first regex don't work with "12345" and the second one with "12345a" Mar 28, 2017 at 11:38
1

I use Regex with easier expression :

re.findall(r"\d{5}", mystring)

It will research 5 numerical digits. But you have to be sure not to have another 5 numerical digits in the string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.