Regex incorrect for huge numbers
droidscout opened this issue · 1 comments
droidscout commented
Hi,
I just found out using the range-regex module in pyton is not working to generate regular expressions when dealing with timestamps.
Let's say I want to generate a regex for values from 1420066800 to 1420153199 (unix timestamps) I get the regex: 1420[0-1][6-5][6-3][8-1]\\d{2} which apparently is wrong.
Using he "re" module in python together with the above regex throws an exception with message: "bad character range".
It would make sense to switch the numbers in a range if the left one is bigger than the right one.
Is there a way to get rid of this bug and update the module?
Thank you in advance.
droidscout commented
Everybody who encounters the same problem, change the function: range_to_pattern to the following:
def range_to_pattern(start, stop):
pattern = ''
any_digit_count = 0
for start_digit, stop_digit in zip(str(start), str(stop)):
if start_digit == stop_digit:
pattern += start_digit
elif start_digit != '0' or stop_digit != '9':
if start_digit > stop_digit:
pattern += '[{}-{}]'.format(stop_digit, start_digit)
else:
pattern += '[{}-{}]'.format(start_digit, stop_digit)
else:
any_digit_count += 1
if any_digit_count:
pattern += r'\d'
if any_digit_count > 1:
pattern += '{{{}}}'.format(any_digit_count)
return pattern