我试图做一个名称验证Regex表达式,以检查名称是否以正确的格式传递。它的格式必须为-“ John”,“ Sarah”,因此,不允许使用空格,不允许使用其他字符,也不允许使用数字。另外,首字母必须为大写字母,其后所有其他字母必须为非大写字母。
我试过编写一些正则表达式,并在regexr上进行检查,但是在那里工作的表达式在C#中无法正常工作
有人可以共享解决方案以排除不是字母的其他任何字符吗?
对我而言,似乎在regexr上使用的regex和在C#中使用的regex之间存在一些差异,这就是为什么在regexr上起作用的表达式在C#中不起作用的原因
先感谢您!:)
编辑:例如:表达式是- ([A-Z]{1}[a-z]*)\w+
。在regexr中,它必须是第一个大写字母,然后是任何其他字母,所有空格或其他字符。当我在C#项目中复制此代码时,它仍然允许编写空格或其他符号,例如。; 等
EDIT2:C#实现如下所示:
string name = nameCapitalLetterCheck_textBox.Text.ToString();
string nameFormat = @"([A-Z]{1}[a-z]*)\w+";
if (Regex.IsMatch(name,nameFormat))
{
nameCapitalLetterCheck_result.Text = "OK";
nameCapitalLetterCheck_result.Foreground = green;
nameCapital_hint.Visibility = Visibility.Hidden;
}
else
{
nameCapitalLetterCheck_result.Text = "Error";
nameCapitalLetterCheck_result.Foreground = red;
nameCapital_hint.Visibility = Visibility.Visible;
}
采用
^\p{Lu}\p{Ll}+\z
见证明
说明
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
\p{Lu} any uppercase character
--------------------------------------------------------------------------------
\p{Ll}+ any lowercase character (1 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
\z the end of the string
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句