我是否可以使用do while
循环和正则表达式来验证字符串输入,以便当数字在[:alpha:]
内时拒绝该字符串
Console.Write("Please Input The student First name/To cancel enter END> ");
StFName[count] = Console.ReadLine();
do
{
Console.Write("Please Input The student First name/To cancel enter END> ");
StFName[count] = Console.ReadLine();
} while (StFName =! "[:alpha:]");
这是我的梦想场景
答案 0 :(得分:1)
do {
Console.Write("Please Input The student First name/To cancel enter END> ");
StFName[count] = Console.ReadLine();
} while (Regex.IsMatch(StfName[count], "\\d+"));
任何带有数字字符的字符串都将无法通过验证。
或者,如果您希望非字母数字(除了空白)字符也失败,您应该使用以下正则表达式:
do {
Console.Write("Please Input The student First name/To cancel enter END> ");
StFName[count] = Console.ReadLine();
} while (!Regex.IsMatch(StfName[count], "[a-zA-Z\\s]+"));
答案 1 :(得分:0)
尝试类似:
cut