Home > Mobile >  Regex match number, a multiple of 5, between 1 to 1000 inclusive
Regex match number, a multiple of 5, between 1 to 1000 inclusive

Time:10-14

I've been facing this problem that I cannot solve.

compiler = re.compile(r'\d*[05]')
result = compiler.findall('0 20 201 95 5 52 35 999 40 19 50 1000 245 1050 118 2500')

Desired output: ['20', '95', '5', '35', '40', '50', '1000', '245']

The closest that I can achieve, based on my code above: ['0', '20', '20', '95', '5', '5', '35', '40', '50', '1000', '245', '1050', '2500']

I am supposed to provide my answer in only the dashed portion of the code re.compile(r'--------').

If someone can help me out that'll be great, thanks.

CodePudding user response:

As pointed out by @depperm

\b(?!\b0\b)(\d{0,2}[05]|1000)\b

would be the perfect regex for your demand.

My idea just works for a single number

^\d{0,2}[05]$|^1000$

Explanation:

  • ^ shows, that the string has to start here
  • /d{0,2} stands for 0,1 or 2 numbers
  • [05] the number 0 or 5 because the all multiples of 5 end with 0 or 5
  • $ end of the string
  • | stands for the operator or
  • ^1000$ stands for the string 1000

but as already pointed out you could look in too other ways as well

CodePudding user response:

You don't need to use regex for this. You can use a comprehension:

result = [i for i in data.split() if not (n := int(i)) % 5 and n <= 1000 and n]

This is equivalent to:

result = []
for i in data.split():
    n = int(i)
    if not n % 5 and n <= 1000 and n:
        result.append(i)

CodePudding user response:

When it's all in one string, we need to add boundaries to our search. Another answer was close, but we can't match by ^ and $ when the single string contains multiple values.

\b or word boundaries makes sure that there aren't additional digits before or after a matching string.

Pattern: \b0\b|(\b\d{0,2}[05]\b|\b1000\b)

All the desired matches are in group 1

https://regex101.com/r/Efhcsb/2

CodePudding user response:

The requirement can be met by using the regex pattern, (\b(?=[^0])\d{0,2}[05])\b|1000.

Demo:

import re

compiler = re.compile(r'(?:\b(?=[^0])\d{0,2}[05])\b|1000')
result = compiler.findall('0 20 201 95 5 52 35 999 40 19 50 1000 245 1050 118 2500')
print(result)

Output:

['20', '95', '5', '35', '40', '50', '1000', '245']

Explanation of the regex:

  • (?: : Start of non-capturing group
    • \b: Word boundary
    • (?=[^0]) : Positive lookahead asserting that there should not be 0 ahead of the current postion
    • \d{0,2} : A digit zero to two times
    • [05] : 0 or 5
  • ) : End of non-capturing group
  • |1000 : OR 1000
  • Related