Home > Enterprise >  regex string match, by max count of digits in that string
regex string match, by max count of digits in that string

Time:04-20

How to, and is it possible at all, in regex to match a string by max N count of digits in that string, digits may be divided by any non digit character(s)?

For instance, string to contain max of 4 digits to be a match. ( 4 just as example it can be as big as 20)

Examples of input sting and to be matching results as follow:

1-1.2abcd   - match (count of digits in a string 3 <= 4)
1a2b3c4d    - match (count of digits 4 <= 4)
123a4       - match (count of digits 4 <= 4)
1a23        - match (you got the idea)
a12         - match
12345       - not match (max num of digits 5 - and since 4 is a max, this is no match)
1a2b3c4d5e  - not match

I've tried different options, tried to dig in regex specs in site like regex101.com, played around with groups and {min,max} but was not able to find a solution.

thank you in advance.

CodePudding user response:

You can describe all the matching strings as having:

  • An optional non-digit prefix
  • Between 0 and 4 sequences of:
    • Exactly 1 digit,
    • An optional non-digit suffix

Translated into a Perl/PCRE-style regular expression, you'd end up with something like this:

^\D*(?:\d\D*){0,4}$

CodePudding user response:

You can do something like that. Keep the pattern simple, just search for digits and get a list of all matches with re.findall. Check the length of that list and you are done.

import re

pattern = re.compile(r"\d")
lst = ["1-1.2abcd", "1a2b3c4d", "123a4", "1a23", "a12", "12345", "1a2b3c4d5e"]

for strings in lst:
    no_of_digits = len(re.findall(pattern, strings))
    if no_of_digits <= 4:
        print('string: ', strings, '- no of digits in it: ', no_of_digits)

Output:
string:  1-1.2abcd - no of digits in it:  3
string:  1a2b3c4d - no of digits in it:  4
string:  123a4 - no of digits in it:  4
string:  1a23 - no of digits in it:  3
string:  a12 - no of digits in it:  2
  • Related