Jason94
Jason94

Reputation: 13620

I can't match a simple username with regex?

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?

Upvotes: 2

Views: 71

Answers (2)

ceth
ceth

Reputation: 754

You don't need the / / in a .net regex.

Upvotes: 0

nneonneo
nneonneo

Reputation: 179717

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.

Upvotes: 7

Related Questions