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?
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.
您不需要/ /
in .net 正则表达式。