简体   繁体   English

我无法将简单的用户名与regex匹配吗?

[英]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? 我忘记了什么?

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. 但在C#中,整个字符串是模式,因此不需要额外的分隔符。

您不需要.net正则表达式中的/ /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM