C#中如何利用正則表達式判斷字符
廢話不多說了,下面代碼給大家介紹下利用正則表達式判斷字符的方法,具體代碼如下所示:
using System;
using System.Text.RegularExpressions;
using System.NET;
namespace 正則表達式檢測字符串
{
 class Program
 {
  static void Main(string[] args)
  {
   Console.WriteLine("請輸入字符串:");
   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]");//正則表達式
   if (reg.IsMatch(str))
   {
    return true;
   }
   else
   {
    return false;
   }
  }
  /// <summary>
  /// 字段串是否為Null或為""(空)
  /// </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>
  /// 檢測是否符合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>
  /// 檢測是否是正確的Url
  /// </summary>
  /// <param name="strUrl">要驗證的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>
  /// 檢測是否有Sql危險字符
  /// </summary>
  /// <param name="str">要判斷字符串</param>
  /// <returns>判斷結(jié)果</returns>
  public static bool IsSafeSqlString(string str)
  {
   return !Regex.IsMatch(str, @"[-|;|,|\/|||
|
|\}|\{|%|@|\*|!|\']");
  }
 }
}
以上所述是小編給大家介紹的C#中如何利用正則表達式判斷字符,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
上一篇:C# TextBox多行文本框的字?jǐn)?shù)限制問題
欄 目:C#教程
下一篇:C#中利用LINQ to XML與反射把任意類型的泛型集合轉(zhuǎn)換成XML格式字符串的方法
本文標(biāo)題:C#中如何利用正則表達式判斷字符
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6093.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
 - 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
 - 01-10C#實現(xiàn)由四周向中心縮小的窗體退出特效
 - 01-10Extjs4如何處理后臺json數(shù)據(jù)中日期和時間
 - 01-10C#中DataGridView常用操作實例小結(jié)
 - 01-10C#編程獲取資源文件中圖片的方法
 - 01-10asp.net中XML如何做增刪改查操作
 - 01-10C#利用反射技術(shù)實現(xiàn)去掉按鈕選中時的邊框效果
 - 01-10C#中查找Dictionary中的重復(fù)值的方法
 - 01-10C#中TreeView實現(xiàn)適合兩級節(jié)點的選中節(jié)點方法
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實現(xiàn)txt定位指定行完整實例
 - 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
 - 01-10C#停止線程的方法
 - 01-10C#實現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 04-02jquery與jsp,用jquery
 - 01-10C#中split用法實例總結(jié)
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10delphi制作wav文件的方法
 


