正則表達式驗證通用方法

1  string strRegex = @"^\d+$";
    if(!Common.CheckRegx(strRegex,strText))
    {
     MessageBox.Show(this.Page,"請輸入正確庫存數量!");
     return;
    }

===========

2 /// <summary>
  /// 正則表達式驗證通用方法
  /// </summary>
  /// <param name="regex">正則表達式</param>
  /// <param name="str">要驗證的字符</param>
  /// <returns>成功返回true,失敗返回false</returns>
  public static Boolean CheckRegx(string regex,string str)
  {
   System.Text.RegularExpressions.Regex strRegex=new Regex(regex);      
   Match m=strRegex.Match(str);
   if(m.Success)
    return true;
   else
    return false;
  }
  public static Boolean CheckRegx(string regex,string[] str)
  {
   System.Text.RegularExpressions.Regex strRegex=new Regex(regex);      
   int err = 0;
   for(int i=0;i<str.Length;i++)
   {
    Match m=strRegex.Match(str[i]);
    if(!m.Success)
     err++;
   }
   if(err == 0)
    return true;
   else
    return false;
  }

原文地址:https://www.cnblogs.com/csj007523/p/1251114.html