Stacey
Stacey

Reputation: 5107

Regular expression between two integers

I'm trying to build a regular expression that will match the word mark to an integer greater or equal to 140000 and less than or equal to 150000.

So, for example, the minimum value would be mark_140000 and the maximum value would be mark_150000. The values mark_139999 and mark_150001 would be invalid.

I have a method that will match from 140000 to 149999:

"^mark_14[0-9]{4}$"

How can I get the maximum to include 150000?

Upvotes: 2

Views: 504

Answers (3)

Jan
Jan

Reputation: 43169

You could write a small function that checks if the number part is in the range:

import re

data = """So for example the minimum value would be mark_140000 and the maximum value would be mark_150000. The values mark_139999 and mark_150001 would be     Many thanks and mark_100000000000000"""

rx = re.compile(r'mark_(\d+)')

def check(number):
    number = float(number)
    if 140000 <= number <= 150000:
        return True
    return False

matches = [match.group(0)
        for match in rx.finditer(data)
        if check(match.group(1))]
print(matches)

This yields

['mark_140000', 'mark_150000']

Upvotes: 0

Mike Tung
Mike Tung

Reputation: 4821

Your regex needs to account for all the possibilities of 140000 to 150000

^mark_1(4\d{4})|(50{4})

Upvotes: 2

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140196

simple enough, just create 2 cases:

"^mark_(14[0-9]{4}|150000)$"

Upvotes: 6

Related Questions