【发布时间】:2016-12-04 21:43:42
【问题描述】:
我正在为 Uni 编写一个需要一些基本验证的程序。
我有一个文本框,允许用户输入他们的年龄。我已经成功编写了一个正则表达式,它检查输入到文本框中的值是否仅包含数字值并且是两个字符长:
Regex agePattern = new Regex(@"^[0-9]{1,2}\z$"); // Age entered must be numeric characters only (0-9) and may only be two charaters long
if (agePattern.IsMatch(ageBox.Text) == false)
{
MessageBox.Show("Customer's age is not valid. Age must be in numeric format & can only be two characters long"); // If Regex pattern & entered string DO NOT match - user get's this message
return;
}
else
{
// do something
}
我的问题是,我可以扩展我的 Regex 表达式以将年龄值限制在 1 到 99 之间吗?
我以前没有写过任何正则表达式,我很挣扎。
谢谢!
【问题讨论】:
-
^[1-9][0-9]?$...
标签: c# regex wpf validation