regexnumbersrangecharacter-class

Why can't a numeric range involving non-single-digit numbers be expressed in a regex character class?


Why is the regex to match numbers from 1 to 10 commonly written as follows?

[1-9]|10

Instead of:

[1-10]

Or this:

[1-(10)]

Solution

  • Sometime a good drawing worth 1000 words...

    Here are the three propositions in your question and the way a regex flavour would understand them:

    [1-9]|10

    Regular expression image

    [1-10]

    Regular expression image

    [1-(10)]

    Invalid regexp !!
    

    This regex is invalid because a range is opened (1-) with a digit but not closed with another digit (ends with ().

    A range is usually bound with digits on both sides or letters on both sides.

    Images generated with Debuggex