Regular expressions, difference between [-] and -

4

I'm working with some regular expressions and I came across the following case:

string pattern = @"^.{2,9}[-].{4}[-].{4}$";
string pattern = @"^.{2,9}-.{4}-.{4}$";

I would like to understand, what is the difference between using the hyphen between [-] and only - ?

    
asked by anonymous 18.12.2017 / 20:04

1 answer

4

The square brackets (brackets [] ) are for specifying lists of characters to match. In this case there is no difference because you are only specifying a character -

But when used with two or more characters, it specifies a list, for example, a list that matches characters between 1 and 5 inclusive: [1-5]

Another example would be a list of a to z : [a-z]

    
18.12.2017 / 20:23