转自:
https://www.cnblogs.com/lip-blog/p/9112285.html
if (Regex.IsMatch(setValue, @"^[+-]?\d*$"))
{
writeVals.Add(Convert.ToInt32(setValue));
writeValsZero.Add(0);
}
else
{
OnException(new ThrowException("Craft:请检查输入是否是整数数值", eErrorCode.OTHER));
statusFlag = (int)statusFlagEnum.FAILE;
return;
}
//对输出值限制进行检查
string strPatten = @"(.*\()(.*)(\).*)";
Regex rex = new Regex(strPatten, RegexOptions.IgnoreCase);
MatchCollection matches = rex.Matches(dataGridViewParam.Rows[i].Cells[0].Value.ToString());
//提取匹配项
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
//string.Format("<br/>{0} 共有 {1} 个分组:{2}<br/>"
// , match.Value, groups.Count, strPatten);
string[] limitValue = groups[2].Value.Split('-');
if (Convert.ToInt32(setValue) < Convert.ToInt32(limitValue[0]) || Convert.ToInt32(setValue) > Convert.ToInt32(limitValue[1]))
{
OnException(new ThrowException("Craft:请检查输入是否超出限制", eErrorCode.OTHER));
statusFlag = (int)statusFlagEnum.FAILE;
return;
}
提取匹配项内的分组信息
//for (int hi = 0; i < groups.Count; i++)
//{
// string s=string.Format("分组 {0} 为 {1},位置为 {2},长度为 {3}<br/>"
// , i
// , groups[i].Value
// , groups[i].Index
// , groups[i].Length);
// MessageBox.Show(s);
//}