C#常用的Regex

來源:互聯網
上載者:User
//驗證使用者輸入的字串中時候只含有數字或字母,漢字   bool IsValidString(string strIn)   {    return Regex.IsMatch(strIn,@"^[A-Za-z0-9\u4e00-\u9fa5]+$");   }//驗證電子郵件   bool IsValidEmail(string strIn)    {     return Regex.IsMatch(strIn,@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");    } //驗證使用者輸入的字串中時候只含有數字或字母   bool IsValidString(string strIn)   {    return Regex.IsMatch(strIn,@"^[A-Za-z0-9]+$");   }/// <summary>   /// 判斷密碼   /// </summary>   /// <param name="PassWord"></param>   /// <returns></returns>   public static bool IsValidPassWord(string PassWord)   {    return Regex.IsMatch(PassWord, @"^(\w){6,20}$");    }/// <summary>   /// 判斷電話/傳真   /// </summary>   /// <param name="Tel"></param>   /// <returns></returns>   public static bool IsValidTel(string Tel)   {    return Regex.IsMatch(Tel, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");    }/// <summary>   /// 判斷手機   /// </summary>   /// <param name="Mobil"></param>   /// <returns></returns>   public static bool IsValidMobil(string Mobil)   {    return Regex.IsMatch(Mobil, @"^(\d)+[-]?(\d){6,12}$");    }/// <summary>   /// 郵遞區號   /// </summary>   /// <param name="Zip"></param>   /// <returns></returns>   public static bool IsValidZip(string Zip)   {    return Regex.IsMatch(Zip, @"^[a-z0-9 ]{3,12}$");    }/// <summary>   /// 判斷日期   /// </summary>   /// <param name="Date"></param>   /// <returns></returns>   public static bool IsValidDate(string Date)   {    bool bValid=Regex.IsMatch(Date, @"^[12]{1}(\d){3}[-][01]?(\d){1}[-][0123]?(\d){1}$");     return (bValid && Date.CompareTo("1753-01-01")>=0);   }//判斷整型   /*public static bool IsValidInt(string Int)   {    return Regex.IsMatch(Int, @"^[1-9]{1}[0-9]{0,6}$");    }*/   /// <summary>   /// 判斷只能輸字母   /// </summary>   /// <param name="EnName"></param>   /// <returns></returns>   public static bool IsValidEnName(string EnName)   {    return Regex.IsMatch(EnName, @"[a-zA-Z]");    }C#判斷字串中是否有漢字using System.Text.RegularExpressions;//先匯入這個使用Regexprivate void isHanzi(string str)        {             Regex reg = new Regex(@"[u4e00-u9fa5]");//Regex            if (reg.IsMatch(str))            {                MessageBox.Show("有漢字");            }            else            {                MessageBox.Show("沒有漢字");            }        }C#驗證是否是合法郵箱先 using System.Text.RegularExpressions;然後按鈕中加入private void button1_Click(object sender, EventArgs e){string reg = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";Regex r = new Regex(reg);if (r.IsMatch(textBox1.Text)){MessageBox.Show("格式正確");}else{MessageBox.Show("不是有效email地址");}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.