[英]C# check if the string contains a character but allowed with other specific character
[英]Check string contains only alphabets in C# and other allowed charaters are: “.”, whitespace, “,”
如何检查字符串在C#中不只包含字母,其他允许的字符包括:点:“。”,空格和逗号:“,”
Regex regex = new Regex("Regex Pattern");
bool result = regex.IsMatch(string);
if(result)
Messagebox.Show("String does not contain alphabets");
例子:
ABC1 =假
ABC =真
ABC ABC =真
abc abc = true
AB ABC =真
原因是因为我有一个包含人名的字段。 通常采用以下格式:
第一
先到后
第一中最后
FM最后
标题。 FM最后
因此,我想验证使用正则表达式的用户。
尝试这个
var regex = new Regex(@"(?i)^[a-z.,\s]+$");
bool res = regex.IsMatch(subject);
这个简单的LINQ表达式将起作用
string name;
bool result = name.All(x => char.IsLetter(x) || x == ' ' || x == '.' || x ==',');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.