.NET Regex Error: [x-y] range in reverse order

后端 未结 3 861
死守一世寂寞
死守一世寂寞 2020-12-06 04:05

I am creating a Regex and so far I did this and tried it,

 ^([0][1-9]|1[0-2])[/-.]

and I get the following error.

      p         


        
相关标签:
3条回答
  • 2020-12-06 04:28

    Not a bug. Inside a character class (denoted by […]) the - character must be first (some flavours allow first or last, I believe) if it is to be included as a literal. Otherwise it is expected to denote a range, such as 0-9 or A-Z or even /-..

    The problem is that according to Unicode, the . comes before the /, so the range is interpreted to be backward, equivalent to specifying a range 7-4.

    If you used [.-/], I would not expect a parse exception, but you wouldn't get the results you expected.

    0 讨论(0)
  • 2020-12-06 04:37

    The problem is with this part:

    [/-.]
    

    That means "the range of characters from '/' to '.'" - but '/' comes after '.' in Unicode, so the range makes no sense.

    If you wanted it to mean "slash, dash or period" then you want:

    [/\-.]
    

    ... in other words, you need to escape the dash. Note that if this is in a regular C# string literal, you'll need to perform another level of escaping too:

    string pattern = "[/\\-.]";
    

    Using a verbatim string literal means you don't need to escape the backslash:

    string pattern = @"[/\-.]";
    

    Alternatively, as Jay suggested, you can just put the dash at the start:

    [-/.]
    

    or end:

    [/.-]
    

    (I've just tested, and all three of these options work.)

    0 讨论(0)
  • 2020-12-06 04:45

    Inside a character class i.e. [] the - denotes a range, i.e. all lower case letters between a and z can be expressed as [a-z].

    What are the range for [/-.]?

    0 讨论(0)
提交回复
热议问题