C#中如何利用正則表達(dá)式判斷字符
廢話不多說(shuō)了,下面代碼給大家介紹下利用正則表達(dá)式判斷字符的方法,具體代碼如下所示:
using System; using System.Text.RegularExpressions; using System.NET; namespace 正則表達(dá)式檢測(cè)字符串 { class Program { static void Main(string[] args) { Console.WriteLine("請(qǐng)輸入字符串:"); string s = Console.ReadLine(); if (GF_IsOk.IsExistHanZi(s)) { Console.Write("包含漢字"); } else { Console.Write("不包含漢字"); } Console.ReadLine(); } } //判斷部分 public class GF_IsOk { /// <summary> /// 判讀是否是IP地址 /// </summary> /// <param name="in_str"></param> /// <returns></returns> public static bool IsIPStr(string in_str) { IPAddress ip; return IPAddress.TryParse(in_str, out ip); } /// <summary> /// 判斷是否是數(shù)字 /// </summary> /// <param name="strNumber"></param> /// <returns></returns> public static bool IsNumber(string strNumber) { Regex objNotNumberPattern = new Regex("[^0-9.-]"); Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*"); Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*"); String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$"; String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$"; Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")"); return !objNotNumberPattern.IsMatch(strNumber) && !objTwoDotPattern.IsMatch(strNumber) && !objTwoMinusPattern.IsMatch(strNumber) && objNumberPattern.IsMatch(strNumber); } /// <summary> /// 判斷是否是日期字符串 /// </summary> /// <param name="in_str"></param> /// <returns></returns> public static bool IsDateStr_yyyymmdd(string in_str) { if (in_str == "") return true; if (in_str.Length != 8) return false; return IsDateStr(in_str); } /// <summary> /// 判斷是否是日期字符串 /// </summary> /// <param name="in_str"></param> /// <returns></returns> public static bool IsDateStr(string in_str) { if (in_str == "") return true; if (in_str.Length == 8) in_str = in_str.Substring(0, 4) + "-" + in_str.Substring(4, 2) + "-" + in_str.Substring(6, 2); DateTime dtDate; bool bValid = true; try { dtDate = DateTime.Parse(in_str); } catch (FormatException) { // 如果解析方法失敗則表示不是日期性數(shù)據(jù) bValid = false; } return bValid; } /// <summary> /// 判斷字符串中是否包含漢字,有返回true 否則為false /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsExistHanZi(string str) { Regex reg = new Regex(@"[\u4e00-\u9fa5]");//正則表達(dá)式 if (reg.IsMatch(str)) { return true; } else { return false; } } /// <summary> /// 字段串是否為Null或?yàn)?"(空) /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsStrNullOrEmpty(string str) { if (str == null || str.Trim() == string.Empty) return true; return false; } /// <summary> /// 返回文件是否存在 /// </summary> /// <param name="filename">文件名</param> /// <returns>是否存在</returns> public static bool IsFileExists(string filename) { return System.IO.File.Exists(filename); } /// <summary> /// 檢測(cè)是否符合email格式 /// </summary> /// <param name="strEmail">要判斷的email字符串</param> /// <returns>判斷結(jié)果</returns> public static bool IsValidEmail(string strEmail) { return Regex.IsMatch(strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]"); } public static bool IsValidDoEmail(string strEmail) { return Regex.IsMatch(strEmail, @"^@(( [0−9]1,3\.[0−9]1,3\.[0−9]1,3\.)|(([\w−]+\.)+))([a−zA−Z]2,4|[0−9]1,3)( ?)$"); } /// <summary> /// 檢測(cè)是否是正確的Url /// </summary> /// <param name="strUrl">要驗(yàn)證的Url</param> /// <returns>判斷結(jié)果</returns> public static bool IsURL(string strUrl) { return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$"); } /// <summary> /// 判斷是否為base64字符串 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsBase64String(string str) { //A-Z, a-z, 0-9, +, /, = return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]"); } /// <summary> /// 檢測(cè)是否有Sql危險(xiǎn)字符 /// </summary> /// <param name="str">要判斷字符串</param> /// <returns>判斷結(jié)果</returns> public static bool IsSafeSqlString(string str) { return !Regex.IsMatch(str, @"[-|;|,|\/||| | |\}|\{|%|@|\*|!|\']"); } } }
以上所述是小編給大家介紹的C#中如何利用正則表達(dá)式判斷字符,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C#讀取txt文件數(shù)據(jù)的方法實(shí)例
讀取txt文本數(shù)據(jù)的內(nèi)容,是我們開發(fā)中經(jīng)常會(huì)遇到的一個(gè)功能,這篇文章主要給大家介紹了關(guān)于C#讀取txt文件數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2021-05-05C#學(xué)習(xí)筆記整理_變量等基礎(chǔ)語(yǔ)法(必看篇)
下面小編就為大家?guī)?lái)一篇C#學(xué)習(xí)筆記整理_變量等基礎(chǔ)語(yǔ)法(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09C# Socket通信的實(shí)現(xiàn)(同時(shí)監(jiān)聽多客戶端)
這篇文章主要介紹了C# Socket通信的實(shí)現(xiàn)(同時(shí)監(jiān)聽多客戶端),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問(wèn)題
這篇文章主要介紹了C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問(wèn)題 ,文中給大家列舉通過(guò)兩種方法來(lái)判斷,需要的朋友可以參考下2018-10-10C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中
這篇文章主要給大家介紹C#.net中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中,本文涉及到C#.net中批量插入數(shù)據(jù)到數(shù)據(jù)庫(kù)中方面的內(nèi)容,對(duì)C#.net批量插入數(shù)據(jù)到數(shù)據(jù)庫(kù)中感興趣的朋友可以參考下本篇文章2015-10-10協(xié)定需要會(huì)話,但是綁定“BasicHttpBinding”不支持它或者因配置不正確而無(wú)法支持它
在IIS7及以上版本服務(wù)器中提供了基于WAS的無(wú).SVC文件的WCF服務(wù)激活功能,能夠提供基于HTTP和非HTTP協(xié)議的訪問(wèn),通過(guò)添加Windows Server AppFabric可以更方便的管理WCF服務(wù)2012-12-12C#以太網(wǎng)Sockets客戶端設(shè)計(jì)實(shí)現(xiàn)
本文主要介紹了C#以太網(wǎng)Sockets客戶端設(shè)計(jì)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02