Why doesn't [01-12] range work as expected?

后端 未结 6 1749
有刺的猬
有刺的猬 2020-11-22 03:34

I\'m trying to use the range pattern [01-12] in regex to match two digit mm, but this doesn\'t work as expected.

6条回答
  •  悲哀的现实
    2020-11-22 04:23

    A character class in regular expressions, denoted by the [...] syntax, specifies the rules to match a single character in the input. As such, everything you write between the brackets specify how to match a single character.

    Your pattern, [01-12] is thus broken down as follows:

    • 0 - match the single digit 0
    • or, 1-1, match a single digit in the range of 1 through 1
    • or, 2, match a single digit 2

    So basically all you're matching is 0, 1 or 2.

    In order to do the matching you want, matching two digits, ranging from 01-12 as numbers, you need to think about how they will look as text.

    You have:

    • 01-09 (ie. first digit is 0, second digit is 1-9)
    • 10-12 (ie. first digit is 1, second digit is 0-2)

    You will then have to write a regular expression for that, which can look like this:

      +-- a 0 followed by 1-9
      |
      |      +-- a 1 followed by 0-2
      |      |
    <-+--> <-+-->
    0[1-9]|1[0-2]
          ^
          |
          +-- vertical bar, this roughly means "OR" in this context
    

    Note that trying to combine them in order to get a shorter expression will fail, by giving false positive matches for invalid input.

    For instance, the pattern [0-1][0-9] would basically match the numbers 00-19, which is a bit more than what you want.

    I tried finding a definite source for more information about character classes, but for now all I can give you is this Google Query for Regex Character Classes. Hopefully you'll be able to find some more information there to help you.

提交回复
热议问题