C#正則函數(shù)用法實例【匹配、替換、提取】
本文實例講述了C#正則函數(shù)用法。分享給大家供大家參考,具體如下:
System.Text.RegularExpressions 命名空間包含一些類,這些類提供對 .NET Framework 正則表達(dá)式引擎的訪問。該命名空間提供正則表達(dá)式功能,可以從運行在 Microsoft .NET Framework 內(nèi)的任何平臺或語言中使用該功能。
1 正則表達(dá)式的常見使用
① 格式匹配
/// <summary>
/// 郵箱格式驗證
/// </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 = "無效郵箱";
}
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; //獲取用戶名
string time = match.Groups["time"].Value; //獲取入職時間
string email = match.Groups["email"].Value; //獲取郵箱地址
string strFormat = String.Format("我是:{0},入職時間:{1},郵箱:{2}", userName, time, email);
result = regex.Replace(str, strFormat); //替換內(nèi)容
Console.WriteLine(result);
}
return result; //結(jié)果:大家好!我是張三,入職時間:2010-10-7,郵箱:zhangsan@163.com 自我介紹。
}
2 我的一個實例
/// <summary>
/// 從XML中提取匹配的字段
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
string strXml = GetStrXml(); //創(chuàng)建用戶信息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; //獲取用戶名
string time = TransDate(matchItem.Groups["time"].Value); //獲取入職時間,并轉(zhuǎn)換日期格式
string email = CheckMail(matchItem.Groups["email"].Value); //獲取郵箱地址,并檢測郵箱格式
string strFormat = String.Format("姓名:{0},入職時間:{1},郵箱:{2}", userName, time, email);
Console.WriteLine(strFormat);
}
}
Console.ReadLine();
}
/// <summary>
/// 創(chuàng)建用戶信息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á)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#正則表達(dá)式用法總結(jié)》、《C#編碼操作技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
C#中datagridview的EditingControlShowing事件用法實例
這篇文章主要介紹了C#中datagridview的EditingControlShowing事件用法,實例分析了datagridview的EditingControlShowing事件的定義與使用技巧,需要的朋友可以參考下2015-06-06
C# 實現(xiàn)與現(xiàn)有.NET事件橋接簡單實例
這篇文章主要介紹了C# 實現(xiàn)與現(xiàn)有.NET事件橋接簡單實例的相關(guān)資料,需要的朋友可以參考下2017-03-03
C#中動態(tài)顯示當(dāng)前系統(tǒng)時間的實例方法
想在網(wǎng)頁中動態(tài)地顯示當(dāng)前系統(tǒng)的時間,找了好多,不過都是一些停在那里不動的。。。不過皇天不負(fù)有心人,終于讓我找到了2013-05-05
DevExpress設(shè)置FocusedNode背景色的方法
這篇文章主要介紹了DevExpress設(shè)置FocusedNode背景色的方法,很實用的功能,需要的朋友可以參考下2014-08-08
Unity UGUI的GridLayoutGroup網(wǎng)格布局組件使用詳解
這篇文章主要介紹了Unity UGUI的GridLayoutGroup網(wǎng)格布局組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

