Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex incorrect for huge numbers #9

Open
droidscout opened this issue Jul 5, 2017 · 1 comment
Open

Regex incorrect for huge numbers #9

droidscout opened this issue Jul 5, 2017 · 1 comment

Comments

@droidscout
Copy link

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
Copy link
Author

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

antonc42 added a commit to antonc42/range-regex that referenced this issue Oct 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant