C#正則函數(shù)用法實(shí)例【匹配、替換、提取】
本文實(shí)例講述了C#正則函數(shù)用法。分享給大家供大家參考,具體如下:
System.Text.RegularExpressions 命名空間包含一些類(lèi),這些類(lèi)提供對(duì) .NET Framework 正則表達(dá)式引擎的訪(fǎng)問(wèn)。該命名空間提供正則表達(dá)式功能,可以從運(yùn)行在 Microsoft .NET Framework 內(nèi)的任何平臺(tái)或語(yǔ)言中使用該功能。
1 正則表達(dá)式的常見(jiàn)使用
① 格式匹配
/// <summary>
/// 郵箱格式驗(yàn)證
/// </summary>
/// <returns></returns>
public static string CheckMail(string strEmail)
{
  string result = "";
  Regex regex = new Regex(@"[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}");
  Match match = regex.Match(strEmail);
  if(match.Success)
  {
    result = strEmail;
  }
  else
  {
    result = "無(wú)效郵箱";
  }
  return result;
}
② 替換匹配內(nèi)容
/// <summary>
/// 轉(zhuǎn)換日期格式(yyyy-MM-dd 轉(zhuǎn) yyyy年MM月dd日)
/// </summary>
/// <param name="strDate"></param>
/// <returns></returns>
public static string TransDate(string strDate)
{
  string result = "";
  Regex regex = new Regex(@"(\d+)-(\d+)-(\d+)");
  if(regex.IsMatch(strDate))
  {
    result = regex.Replace(strDate,"$1年$2月$3日");
  }
  return result;
}
③ 提取匹配內(nèi)容
/// <summary>
/// 正則表達(dá)式提取和替換內(nèi)容
/// </summary>
public static string Contentextract()
{
  string result = "";
  string str = "大家好! <User EntryTime='2010-10-7' Email='zhangsan@163.com'>張三</User> 自我介紹。";
  Regex regex = new Regex(@"<User\s*EntryTime='(?<time>[\s\S]*?)'\s+Email='(?<email>[\s\S]*?)'>(?<userName>[\s\S]*?)</User>", RegexOptions.IgnoreCase);
  Match match = regex.Match(str);
  if(match.Success)
  {
    string userName = match.Groups["userName"].Value; //獲取用戶(hù)名
    string time = match.Groups["time"].Value; //獲取入職時(shí)間
    string email = match.Groups["email"].Value; //獲取郵箱地址
    string strFormat = String.Format("我是:{0},入職時(shí)間:{1},郵箱:{2}", userName, time, email);
    result = regex.Replace(str, strFormat); //替換內(nèi)容
    Console.WriteLine(result);
  }
  return result;  //結(jié)果:大家好!我是張三,入職時(shí)間:2010-10-7,郵箱:zhangsan@163.com 自我介紹。
}
2 我的一個(gè)實(shí)例
/// <summary>
/// 從XML中提取匹配的字段
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
  string strXml = GetStrXml(); //創(chuàng)建用戶(hù)信息XML
  Regex userRegex = new Regex(@"<User\s*EntryTime='(?<time>[\s\S]*?)'\s+Email='(?<email>[\s\S]*?)'>(?<userName>[\s\S]*?)</User>", RegexOptions.IgnoreCase);
  MatchCollection userMatchColl = userRegex.Matches(strXml);
  if (userMatchColl.Count > 0)
  {
    foreach (Match matchItem in userMatchColl)
    {
      string userName = matchItem.Groups["userName"].Value; //獲取用戶(hù)名
      string time = TransDate(matchItem.Groups["time"].Value); //獲取入職時(shí)間,并轉(zhuǎn)換日期格式
      string email = CheckMail(matchItem.Groups["email"].Value); //獲取郵箱地址,并檢測(cè)郵箱格式
      string strFormat = String.Format("姓名:{0},入職時(shí)間:{1},郵箱:{2}", userName, time, email);
      Console.WriteLine(strFormat);
    }
  }
  Console.ReadLine();
}
/// <summary>
/// 創(chuàng)建用戶(hù)信息XML
/// </summary>
/// <returns></returns>
public static string GetStrXml()
{
  StringBuilder strXml = new StringBuilder();
  strXml.Append("<UserInfo>");
  strXml.Append("<User EntryTime='2010-10-7' Email='zhangsan@163.com'>張三</User>");
  strXml.Append("<User EntryTime='2012-5-15' Email='lisi163.com'>李四</User>");
  strXml.Append("<User EntryTime='2012-6-13' Email='wangwu@qq.com'>王五</User>");
  strXml.Append("</UserInfo>");
  return strXml.ToString();
}
PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線(xiàn)測(cè)試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線(xiàn)生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《C#正則表達(dá)式用法總結(jié)》、《C#編碼操作技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》及《C#程序設(shè)計(jì)之線(xiàn)程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
上一篇:C# winform打開(kāi)Excel文檔的方法總結(jié)(必看篇)
欄 目:C#教程
下一篇:C#獲取鼠標(biāo)在listview右鍵點(diǎn)擊單元格的內(nèi)容方法
本文標(biāo)題:C#正則函數(shù)用法實(shí)例【匹配、替換、提取】
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5986.html
您可能感興趣的文章
- 01-10C#線(xiàn)程隊(duì)列用法實(shí)例分析
 - 01-10C#中Socket通信用法實(shí)例詳解
 - 01-10C#中的事務(wù)用法實(shí)例分析
 - 01-10C#中Arraylist的sort函數(shù)用法實(shí)例分析
 - 01-10C#中yield用法使用說(shuō)明
 - 01-10C#正則表達(dá)式的6個(gè)簡(jiǎn)單例子
 - 01-10C#數(shù)據(jù)庫(kù)操作的用法
 - 01-10C#二維數(shù)組基本用法實(shí)例
 - 01-10C#中using指令的幾種用法
 - 01-10C#中的正則表達(dá)式介紹
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于A(yíng)SP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線(xiàn)程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 01-10C#中split用法實(shí)例總結(jié)
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 04-02jquery與jsp,用jquery
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 01-10delphi制作wav文件的方法
 - 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
 


