我正在学习C#和 我有一个小测试程序,控制台应该接收一个数字作为输入而不是字母字符。查询正则表达式对象检查指定的字符串模式
string inputString;
string pattern = "[A-Za-z]*";
Regex re = new Regex(pattern);
inputString = Console.ReadLine();
while(re.Match(inputString).Success)
{
Console.WriteLine("Please stick to numerals");
inputString = Console.ReadLine();
}
Console.WriteLine(inputString);
问题是编译器不区分字母或数字。
任何建议也许 代码似乎是正确的。
我可能总体上首选检查所有数字而不是查找字符:'“^ [0-9] + $”'(我的RegEx虽然不是很好,但这可能是错误的) – musefan 2012-04-17 08:00:59
@musefan我更新了我的答案与数字替代检查。 – stema 2012-04-17 08:13:35