Regular expressions, difference between [-] and -

Asked

Viewed 164 times

4

I am 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 hifén [-] or only -?

1 answer

4


The square brackets (square brackets []) serve to specify character lists to validate (match). In this case there is no difference because you are only specifying one 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]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.