2

It's very simple:

Match match = Regex.Match(username, @"/^[a-z0-9_-]{3,16}$/", RegexOptions.IgnoreCase);
if (!match.Success)
    throw new Exception("Manglende/ugyldig brukernavn.");

But no matter what it fails that test. What am I forgetting?

4

2 回答 2

7

Don't put slashes around the regex:

@"^[a-z0-9_-]{3,16}$"

The slashes are typically used in other languages to delimit a regex. But in C#, the entire string is the pattern so additional delimiters are not required.

于 2013-03-07T07:58:57.593 回答
0

您不需要/ /in .net 正则表达式。

于 2013-03-07T08:01:37.560 回答