ok, new to regular expressions so can someone please explain something to me. In trying to create an expression to check if an entry is between 1 and 15, I first came up with:
[1-9]|1[0-5]
this didn't work however
1[0-5]?|[2-9]
did work.

I understand why the second one works, but not why the first one didn't.

Dani AI

Generated

As found and as began to explain, the behaviour comes down to two things: alternation is tried left-to-right, and many regex APIs return a match for any substring unless you force a full-string match. That means a single-digit alternative can "win" on input like 10 by matching the leading 1 and stopping, so the engine reports success even though the whole string wasn't the intended match.

In Python it's simple and clearer to avoid the alternation trap: parse the input as an integer and test the range. That is robust, fast, and easier to read than tricky alternations.

def is_1_to_15(s):
    s = s.strip()
    try:
        n = int(s)
    except ValueError:
        return False
    # rejects leading zeros like "01"; remove the str(n) check if you want to accept them
    return 1 <= n <= 15 and str(n) == s

If you must use a regex, force a full-string match or use the language's "full match" function rather than a search/match that allows partial matches. Another safe regex approach is to build the alternation programmatically so you enumerate exactly the allowed values and then full-match that pattern:

import re
pattern = re.compile(r'^(?:' + '|'.join(map(str, range(1, 16))) + r')$')
def is_valid(s):
    return bool(pattern.fullmatch(s.strip()))

Troubleshooting tips: trim whitespace first, decide whether leading zeros should be allowed, and pick full-string matching (or anchors) rather than relying on partial matches. For validation of numeric ranges, prefer integer checks in code — they avoid subtle regex ordering and backtracking pitfalls.

Recommended Answers

All 2 Replies

k... I'm not a RexEx guru either but I'm going to give it a shot, bolded blue text text will be subject of my explanation when regex expressions are used...

your expression [1-9]|1[0-5]

Scenario A: [1-9]|1[0-5] means match any number 1 thru 9, pretty basic
[1-9]|1[0-5] is a logical operator [OR]
Scenario B:[1-9]|1[0-5] means any number zero thru five

ok now that we broke it down lets look at what your expression would match...
1 = match based on scenario A
2 = match basesed on A
... up to 9 would match!
10 = now comes the fun part the 1 would match Scenario A and since there was a match it doesnt evaluate it under scenario B and 0 gets ignored.
11 = again Scenario A twice and doesn't even get to scenario B

so for it two work you must have it backwards
1[0-5]|[1-9]

and I would even go a step further and make sure the match is not in the middle of a string by adding ^ in the begging of the expression to indicate begining of line or a word and $ at the end of the expression to indicated end of line or word, like so...
(^1[0-5]$)|(^[1-9]$)

I hope this helps. :cool:

cool thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.