詳解C#正則表達式Regex經常使用婚配。本站提示廣大學習愛好者:(詳解C#正則表達式Regex經常使用婚配)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解C#正則表達式Regex經常使用婚配正文
應用Regex類須要援用定名空間:using System.Text.RegularExpressions;
1、應用Regex類完成驗證
示例1:正文的代碼所起的感化是雷同的,不外一個是靜態辦法,一個是實例辦法
var source = "劉備關羽張飛孫權何問起";
//Regex regex = new Regex("孫權");
//if (regex.IsMatch(source))
//{
// Console.WriteLine("字符串中包括有敏感詞:孫權!");
//}
if (Regex.IsMatch(source, "孫權"))
{
Console.WriteLine("字符串中包括有敏感詞:孫權!");
}
Console.ReadLine();
示例2:應用帶兩個參數的結構函數,第二個參數指導疏忽年夜小寫,很經常使用
var source = "123abc345DEf";
Regex regex = new Regex("def",RegexOptions.IgnoreCase);
if (regex.IsMatch(source))
{
Console.WriteLine("字符串中包括有敏感詞:def!");
}
Console.ReadLine();
2、應用Regex類停止調換
示例1:簡略情形
var source = "123abc456ABC789";
// 靜態辦法
//var newSource=Regex.WordStr(source,"abc","|",RegexOptions.IgnoreCase);
// 實例辦法
Regex regex = new Regex("abc", RegexOptions.IgnoreCase);
var newSource = regex.WordStr(source, "|");
Console.WriteLine("原字符串:"+source);
Console.WriteLine("調換後的字符串:" + newSource);
Console.ReadLine();
成果:
原字符串:123abc456ABC789
調換後的字符串:123|456|789
示例2:將婚配到的選項調換為html代碼,我們應用了MatchEvaluator拜托
var source = "123abc456ABCD789";
Regex regex = new Regex("[A-Z]{3}", RegexOptions.IgnoreCase);
var newSource = regex.WordStr(source,new MatchEvaluator(OutPutMatch));
Console.WriteLine("原字符串:"+source);
Console.WriteLine("調換後的字符串:" + newSource);
Console.ReadLine();
//柔城
private static string OutPutMatch(Match match)
{
return "<b>" +match.Value+ "</b>";
}
輸入:
原字符串:123abc456ABCD789
調換後的字符串:123<b>abc</b>456<b>ABC</b>D789
3、C#正則表達式Regex經常使用婚配
#region 身份證號碼正則表達式
//何問起
Console.WriteLine("請輸出一個身份證號碼");
string id = Console.ReadLine();
bool b4 = Regex.IsMatch(id, @"^\d{15}|\d{18}$");
bool b5 = Regex.IsMatch(id, @"^(\d{15}|\d{18})$");
Console.WriteLine(b4);
Console.WriteLine(b5);
#endregion
#region 婚配德律風號碼
//hovertree
Console.WriteLine("請輸出德律風號碼");
string phone = Console.ReadLine();
bool b = Regex.IsMatch(phone, @"^((\d{3,4}\-\d?{7,8})|(\d{5}))$");
Console.WriteLine(b);
#endregion
#region 婚配email的regex
//hovertree
Console.WriteLine("請輸出Email地址");
string email = Console.ReadLine();
bool bhvt = Regex.IsMatch(email, @"^\w+@\w+\.\w+$");
Console.WriteLine(bhvt);
#endregion
#region 婚配ip地址的regex
//hovertree
Console.WriteLine("請輸出一個IP地址");
string ip = Console.ReadLine();
bool bkly = Regex.IsMatch(ip, @"^\d{1,3}(\.\d{1,3}){3}$");
Console.WriteLine(bkly);
#endregion
#region 婚配日期正當regex
//何問起
Console.WriteLine("請輸出一個日期");
string date = Console.ReadLine();
bool bhovertree = Regex.IsMatch(date, @"^\d{4}\-\d{1,2}\-\d{1,2}$");
Console.WriteLine(bhovertree);
#endregion
#region 婚配url地址的regex
//"http://hovertree.com"
//"http://keleyi.com/a/bjae/h1o76nuh.htm?id=3&name=aaa"
//"https://s.taobao.com/search?q=hover+tree&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20151204&ie=utf8"
//"ftp://127.0.0.1/myslider.txt"
//hovertree
Console.WriteLine("請輸出url地址");
string url = Console.ReadLine();
bool bkeleyi = Regex.IsMatch(url, @"^[a-zA-Z]+://.+$");
Console.WriteLine(bkeleyi);
#endregion
以上就是關於C#正則表達式Regex經常使用婚配的具體引見,願望對年夜家的進修有所贊助。