很简单:
Match match = Regex.Match(username, @"/^[a-z0-9_-]{3,16}$/", RegexOptions.IgnoreCase);
if (!match.Success)
throw new Exception("Manglende/ugyldig brukernavn.");
但不管它通过什么测试。我忘记了什么?
最佳答案
不要在正则表达式周围放置斜线:
@"^[a-z0-9_-]{3,16}$"
其他语言中通常使用斜杠来分隔正则表达式。但在 C# 中,整个字符串就是模式,因此不需要额外的分隔符。
关于c# - 我无法将简单的用户名与正则表达式匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15265909/