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

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.


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 [/-.]?


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.)

Tags:

C#

Regex