[英]Regex match first two occurrences of a capital letter followed by several lower case
[英]C# Regex first letter capital the rest lower case
我正在尝试编写一个正则表达式,如果第一个字母大写且其余字母均为小写,则返回true。 但是,我编写的方法始终返回false。 我的正则表达式有什么问题,我应该进行哪些更改。 这是我的代码。
public bool VerifyName(string name){
Regex rgx = new Regex("^[A-Z][a-z]+$");
return rgx.Equals(name);
}
您正在使用Equals
方法,它会比较您的字符串与正则表达式对象的平等 。 这将永远是不对的,就像比较苹果和桔子。 请改用IsMatch
。
您还可以通过添加Unicode支持来改进正则表达式:
^\p{Lu}\p{Ll}*$
如果我们简化一下代码,则会得到:
public bool VerifyName(string name)
=> Regex.IsMatch(name, @"^\p{Lu}\p{Ll}*$");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.