【轉】http://blog.csdn.net/dingxiaowei2013/article/details/8571757
1 using System; 2 using System.Text.RegularExpressions; 3 using System.NET; 4 5 namespace 正則表達式檢測字符串 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 12 Console.WriteLine("請輸入字符串:"); 13 string s = Console.ReadLine(); 14 if (GF_IsOk.IsExistHanZi(s)) 15 { 16 Console.Write("包含漢字"); 17 } 18 else 19 { 20 Console.Write("不包含漢字"); 21 } 22 Console.ReadLine(); 23 } 24 } 25 //判斷部分 26 public class GF_IsOk 27 { 28 /// <summary> 29 /// 判讀是否是IP地址 30 /// </summary> 31 /// <param name="in_str"></param> 32 /// <returns></returns> 33 public static bool IsIPStr(string in_str) 34 { 35 IPAddress ip; 36 return IPAddress.TryParse(in_str, out ip); 37 } 38 39 /// <summary> 40 /// 判斷是否是數字 41 /// </summary> 42 /// <param name="strNumber"></param> 43 /// <returns></returns> 44 public static bool IsNumber(string strNumber) 45 { 46 47 Regex objNotNumberPattern = new Regex("[^0-9.-]"); 48 Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*"); 49 Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*"); 50 String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$"; 51 String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$"; 52 Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")"); 53 return !objNotNumberPattern.IsMatch(strNumber) && 54 !objTwoDotPattern.IsMatch(strNumber) && 55 !objTwoMinusPattern.IsMatch(strNumber) && 56 objNumberPattern.IsMatch(strNumber); 57 } 58 59 /// <summary> 60 /// 判斷是否是日期字符串 61 /// </summary> 62 /// <param name="in_str"></param> 63 /// <returns></returns> 64 public static bool IsDateStr_yyyymmdd(string in_str) 65 { 66 if (in_str == "") return true; 67 if (in_str.Length != 8) return false; 68 return IsDateStr(in_str); 69 } 70 71 /// <summary> 72 /// 判斷是否是日期字符串 73 /// </summary> 74 /// <param name="in_str"></param> 75 /// <returns></returns> 76 public static bool IsDateStr(string in_str) 77 { 78 if (in_str == "") return true; 79 if (in_str.Length == 8) 80 in_str = in_str.Substring(0, 4) + "-" + in_str.Substring(4, 2) + "-" + in_str.Substring(6, 2); 81 DateTime dtDate; 82 bool bValid = true; 83 try 84 { 85 dtDate = DateTime.Parse(in_str); 86 } 87 catch (FormatException) 88 { 89 // 如果解析方法失敗則表示不是日期性數據 90 bValid = false; 91 } 92 return bValid; 93 } 94 95 /// <summary> 96 /// 判斷字符串中是否包含漢字,有返回true 否則為false 97 /// </summary> 98 /// <param name="str"></param> 99 /// <returns></returns> 100 public static bool IsExistHanZi(string str) 101 { 102 Regex reg = new Regex(@"[\u4e00-\u9fa5]");//正則表達式 103 if (reg.IsMatch(str)) 104 { 105 return true; 106 } 107 else 108 { 109 return false; 110 } 111 } 112 113 114 /// <summary> 115 /// 字段串是否為Null或為""(空) 116 /// </summary> 117 /// <param name="str"></param> 118 /// <returns></returns> 119 public static bool IsStrNullOrEmpty(string str) 120 { 121 if (str == null || str.Trim() == string.Empty) 122 return true; 123 124 return false; 125 } 126 127 /// <summary> 128 /// 返回文件是否存在 129 /// </summary> 130 /// <param name="filename">文件名</param> 131 /// <returns>是否存在</returns> 132 public static bool IsFileExists(string filename) 133 { 134 return System.IO.File.Exists(filename); 135 } 136 137 138 /// <summary> 139 /// 檢測是否符合email格式 140 /// </summary> 141 /// <param name="strEmail">要判斷的email字符串</param> 142 /// <returns>判斷結果</returns> 143 public static bool IsValidEmail(string strEmail) 144 { 145 return Regex.IsMatch(strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]"); 146 } 147 148 public static bool IsValidDoEmail(string strEmail) 149 { 150 return Regex.IsMatch(strEmail, @"^@(( 151 152 [0−9]1,3\.[0−9]1,3\.[0−9]1,3\.)|(([\w−]+\.)+))([a−zA−Z]2,4|[0−9]1,3)( 153 ?)$"); 154 } 155 /// <summary> 156 /// 檢測是否是正確的Url 157 /// </summary> 158 /// <param name="strUrl">要驗證的Url</param> 159 /// <returns>判斷結果</returns> 160 public static bool IsURL(string strUrl) 161 { 162 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\.\,\?\'\\\+&%\$#\=~_\-]+))*$"); 163 } 164 165 166 167 /// <summary> 168 /// 判斷是否為base64字符串 169 /// </summary> 170 /// <param name="str"></param> 171 /// <returns></returns> 172 public static bool IsBase64String(string str) 173 { 174 //A-Z, a-z, 0-9, +, /, = 175 return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]"); 176 } 177 178 /// <summary> 179 /// 檢測是否有Sql危險字符 180 /// </summary> 181 /// <param name="str">要判斷字符串</param> 182 /// <returns>判斷結果</returns> 183 public static bool IsSafeSqlString(string str) 184 { 185 return !Regex.IsMatch(str, @"[-|;|,|\/||| 186 187 | 188 |\}|\{|%|@|\*|!|\']"); 189 } 190 } 191 }