2016-03-17 88 views
0

我可以用一個do while環和正則表達式驗證字符串輸入,因此該字符串被拒絕時,一個數字是內部與[:alpha:]字符串驗證 - 用do while循環

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

你的問題不太清楚。 –

+0

@LibertyLocked:它類似於POSIX中的'[a-zA-Z]'。 [Here](http://www.regular-expressions.info/posixbrackets.html) –

回答

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]+")); 
+0

頂級作品完美謝謝 – user3019354

0

嘗試類似:

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 (!Regex.IsMatch(StfName[count], "[[:alpha:]]"));