柚子快報(bào)激活碼778899分享:C#正則表達(dá)式的使用
柚子快報(bào)激活碼778899分享:C#正則表達(dá)式的使用
C#正則表達(dá)式 System.Text.RegularExpressions.Regex
使用時(shí)需要引入命名空間 using System.Text.RegularExpressions;
如果不引用則寫成 System.Text.RegularExpressions.Regex 使用方法如下:
string str="測試=123456";
string result="";
result= System.Text.RegularExpressions.Regex.Replace(str, @"[^0-9]+", "");//匹配非數(shù)字字符,用空字符串代替
//print 123456
(1)@符號(hào) 在C#中""為轉(zhuǎn)義字符,@符號(hào)用于忽略轉(zhuǎn)義字符,如涉及到轉(zhuǎn)義字符或路徑
string testStr1="\\r\\n";
string testStr2=@"\r\n";
string path1= "D:\\新建文件夾\\新建文件.txt";
string path2 = @"D:\新建文件夾\新建文件.txt";
(2)基本語法字符
符號(hào)含義\d0-9的數(shù)字\D\d的補(bǔ)集,所有非數(shù)字的字符(同[^0-9])\w單詞字符,指大小寫字母、0-9數(shù)字、下劃線\W\w的補(bǔ)集\s空白字符,包括換行符\n、回車符\r、制表符\t、垂直制表符\v、換頁符\f\S\s的補(bǔ)集.除換行符\n以外的任意字符[…]匹配[]內(nèi)所列出的所有字符[^…]匹配非[]內(nèi)所列出的所有字符
舉例:
string context = "這段文字共2329個(gè)字";
int count = 0;
count = int.Parse(System.Text.RegularExpressions.Regex.Replace(context1, @"\D", ""));//其中@"\D"可用@"[^0-9]"代替
string i = "saft";
string m = "2134";
System.Text.RegularExpressions.Regex t = new System.Text.RegularExpressions.Regex(@"\D");//指定表達(dá)式
bool b=t.IsMatch(i);//與表達(dá)式是否匹配 true
b=t.IsMatch(m);//false
(3)定位字符 定位字符所代表的是一個(gè)虛的字符,代表一個(gè)位置
舉例:
string n = "Live for nothing,die for something";
string m = "Live for nothing,die for some thing";
Regex r1 = new Regex(@"\bthing\b");
Console.WriteLine("r1 match count:" + r1.Matches(n).Count);//0 r1中的字符在n中出現(xiàn)的次數(shù)
Regex r2 = new Regex(@"thing\b");
Console.WriteLine("r2 match count:" + r2.Matches(n).Count);//2
Regex r3 = new Regex(@"\bthing\b");
Console.WriteLine("r3 match count:" + r3.Matches(m).Count);//1
(4)重復(fù)描述字符
舉例:
string x = "100";
Regex r = new Regex(@"^\d{1}[1-9]?\d{2}$");//同 @"^\+?[1-9]?\d{2}$"
int i = r.Matches(x).Count;//1
(5)擇一匹配 [0-9]也是屬于一種擇一匹配,但是只能匹配單個(gè)字符,符號(hào)(|)提供了更大的范圍,例如(ab|xy)表示匹配ab或xy,注意"|“與”()"是一個(gè)整體。
舉例:
//匹配[0,100.0]之間的數(shù)字,含小數(shù)點(diǎn)的數(shù)字小數(shù)點(diǎn)后字符不能缺省
string a = "100";
string b = "99.28";
string c = "100.02";
string d = "100.0";
Regex r = new Regex(@"^\+?((100(.0+)*)|[1-9]?[1-9](\.\d+)*)$");
int i = r.Matches(a).Count;//1
int j = r.Matches(b).Count;//1
int k = r.Matches(c).Count;//0
int l = r.Matches(d).Count;//1
(6)特殊字符匹配
舉例:
// 匹配\
string x = "\\";
Regex r1 = new Regex(@"^\\$");
int i = r1.Matches(x).Count;//1
Regex r2 = new Regex("^\\\\$");
int j = r2.Matches(x).Count;//1
// 匹配"
string y = "\"";
Regex r0 = new Regex("^\"$");
int k = r0.Matches(y).Count;//1
Regex r = new Regex(@"^""$");
int l = r.Matches(y).Count;//1
(7)組與非捕獲組
舉例:
//正則表達(dá)式引擎會(huì)記憶“()”中匹配到的內(nèi)容,作為一個(gè)“組”,并且可以通過索引的方式進(jìn)行引用。表達(dá)式中的“\1”,用于反向引用表達(dá)式中出現(xiàn)的第一個(gè)組,即粗體標(biāo)識(shí)的第一個(gè)括號(hào)內(nèi)容,“\2”則依此類推。
string x = "Live for nothing,die for something";
string y = "Live for nothing,die for somebody";
Regex r = new Regex(@"^Live ([a-z]{3}) no([a-z]{5}),die \1 some\2$");
Console.WriteLine("x match count:" + r.Matches(x).Count);//1
Console.WriteLine("y match count:" + r.Matches(y).Count);//0
//獲取組中的內(nèi)容。注意,此處是Groups[1],因?yàn)镚roups[0]是整個(gè)匹配的字符串,即整個(gè)變量x的內(nèi)容。
string x = "Live for nothing,die for something";
Regex r = new Regex(@"^Live for no([a-z]{5}),die for some\1$");
if (r.IsMatch(x))
{Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//輸出:thing}
//可根據(jù)組名進(jìn)行索引。使用以下格式為標(biāo)識(shí)一個(gè)組的名稱(?
string x = "Live for nothing,die for something";
Regex r = new Regex(@"^Live for no(?
if (r.IsMatch(x))
{Console.WriteLine("group1 value:" + r.Match(x).Groups["g1"].Value); }//輸出:thing
//刪除原字符串中重復(fù)出現(xiàn)的“nothing”。在表達(dá)式之外,使用“$1”來引用第一個(gè)組,下面則是通過組名來引用:
string x = "Live for nothing nothing";
Regex r = new Regex(@"([a-z]+) \1");
if (r.IsMatch(x))
{x = r.Replace(x, "$1");
Console.WriteLine("var x:" + x);//輸出:Live for nothing}
string x = "Live for nothing nothing";
Regex r = new Regex(@"(?
if (r.IsMatch(x))
{x = r.Replace(x, "${g1}");
Console.WriteLine("var x:" + x);//輸出:Live for nothing}
//在組前加上“?:”表示這是個(gè)“非捕獲組”,即引擎將不保存該組的內(nèi)容。
string x = "Live for nothing";
Regex r = new Regex(@"^Live for no(?:[a-z]{5})$");
if (r.IsMatch(x))
{Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//輸出:(空)}
(8)貪婪與非貪婪
正則表達(dá)式的引擎是貪婪,只要模式允許將匹配盡可能多的字符,通過在“重復(fù)描述字符”(*,+)后面添加“?”,可以將匹配模式改為非貪婪。 舉例:
string x = "Live for nothing,die for something";
Regex r1 = new Regex(@".*thing");
if (r1.IsMatch(x))
{
Console.WriteLine("match:" + r1.Match(x).Value);//輸出:Live for nothing,die for something}
Regex r2 = new Regex(@".*?thing");
if (r2.IsMatch(x))
{
Console.WriteLine("match:" + r2.Match(x).Value); //輸出:Live for nothing}
}
}
(9)回溯與非回溯
使用“(?>…)”方式進(jìn)行非回溯聲明,由于正則表達(dá)式引擎的貪婪特性,導(dǎo)致它在某些情況下進(jìn)行回溯已獲得匹配。 舉例:
string x = "Live for nothing,die for something";
Regex r1 = new Regex(@".*thing,");
if (r1.IsMatch(x))
{
Console.WriteLine("match:" + r1.Match(x).Value); //輸出:Live for nothing, }
Regex r2 = new Regex(@"(?>.*)thing,");
if (r2.IsMatch(x))//不匹配
{
Console.WriteLine("match:" + r2.Match(x).Value);
}
//在r1中,“.*”由于其貪婪特性,將一直匹配到字符串的最后,隨后匹配“thing”,但在匹配“,”時(shí)失敗,此時(shí)引擎將回溯,并在“thing,”處匹配成功。在r2中,由于強(qiáng)制非回溯,所以整個(gè)表達(dá)式匹配失敗。
}
(10)正向預(yù)搜索、反向預(yù)搜索
正向預(yù)搜索聲明格式:正聲明 “(?=…)”,負(fù)聲明 “(?!..)” ,聲明本身不作為最終匹配結(jié)果的一部分。 舉例
string x = "1024 used 2048 free";
Regex r1 = new Regex(@"\d{4}(?= used)");
if (r1.Matches(x).Count == 1)
{
Console.WriteLine("r1 match:" + r1.Match(x).Value);//輸出:1024}
Regex r2 = new Regex(@"\d{4}(?! used)");
if (r2.Matches(x).Count == 1)
{
Console.WriteLine("r2 match:" + r2.Match(x).Value); //輸出:2048}
}
//r1中的正聲明表示必須保證在四位數(shù)字的后面必須緊跟著“ used”,r2中的負(fù)聲明表示四位數(shù)字之后不能跟有“ used”。
}
反向預(yù)搜索聲明格式:正聲明“(?<=)”,負(fù)聲明“(?
string x = "used:1024 free:2048";
Regex r1 = new Regex(@"(?<=used:)\d{4}");
if (r1.Matches(x).Count == 1)
{
Console.WriteLine("r1 match:" + r1.Match(x).Value);//輸出:1024}
Regex r2 = new Regex(@"(?
if (r2.Matches(x).Count == 1)
{
Console.WriteLine("r2 match:" + r2.Match(x).Value);//輸出:2048}
}
//r1中的反向正聲明表示在4位數(shù)字之前必須緊跟著“used:”,r2中的反向負(fù)聲明表示在4位數(shù)字之前必須緊跟著除“used:”之外的字符串。
(11)十六進(jìn)制字符范圍
正則表達(dá)式中,可以使用 “\xXX” 和 “\uXXXX” 表示一個(gè)字符(“X” 表示一個(gè)十六進(jìn)制數(shù))形式字符范圍: \xXX 編號(hào)在 0到255 范圍的字符,比如:空格可以使用 “\x20” 表示。 \uXXXX 任何字符可以使用 “\u” 再加上其編號(hào)的4位十六進(jìn)制數(shù)表示,比如:漢字可以使用“[\u4e00-\u9fa5]”表示。
(12)值域完備匹配特殊考慮
對(duì)[0,100]的比較完備的匹配需要特殊考慮的地方包括 *00合法,00.合法,00.00合法,001.100合法 *空字符串不合法,僅小數(shù)點(diǎn)不合法,大于100不合法 *數(shù)值是可帶后綴的,如“1.07f”表示該值為一個(gè)float類型(未考慮)
Regex r = new Regex(@"^\+?0*(?:100(\.0*)?|(\d{0,2}(?=\.\d)|\d{1,2}(?=($|\.$)))(\.\d*)?)$");
1
(13)精確匹配困難
有些需求要做到精確匹配比較困難,例如:日期、Url、Email地址等,其中一些你甚至需要研究一些專門的文檔寫出精確完備的表達(dá)式,對(duì)于這種情況,只能退而求其次,保證比較精確的匹配。例如對(duì)于日期,可以基于應(yīng)用系統(tǒng)的實(shí)際情況考慮一段較短的時(shí)間,或者對(duì)于像Email的匹配,可以只考慮最常見的形式。
柚子快報(bào)激活碼778899分享:C#正則表達(dá)式的使用
參考文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。