欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 正則表達式經(jīng)典分類整理集合手冊第2/3頁

 更新時間:2009年03月28日 00:47:16   作者:  
現(xiàn)在用到正則的時候也比較少,把以前的筆記等整理一下,以志不忘。

(4)重復(fù)描述字符
“重復(fù)描述字符”是體現(xiàn)C#正則表達式“很好很強大”的地方之一:
{n} 匹配前面的字符n次
{n,} 匹配前面的字符n次或多于n次
{n,m} 匹配前面的字符n到m次
? 匹配前面的字符0或1次
+ 匹配前面的字符1次或多于1次
* 匹配前面的字符0次或式于0次
以下提供一些簡單的示例:
復(fù)制代碼 代碼如下:

string x = "1024";
string y = "+1024";
string z = "1,024";
string a = "1";
string b="-1024";
string c = "10000";
Regex r = new Regex(@"^\+?[1-9],?\d{3}$");
Console.WriteLine("x match count:" + r.Matches(x).Count);//1
Console.WriteLine("y match count:" + r.Matches(y).Count);//1
Console.WriteLine("z match count:" + r.Matches(z).Count);//1
Console.WriteLine("a match count:" + r.Matches(a).Count);//0
Console.WriteLine("b match count:" + r.Matches(b).Count);//0
Console.WriteLine("c match count:" + r.Matches(c).Count);//0
//匹配1000到9999的整數(shù)。

(5)擇一匹配
C#正則表達式中的 (|) 符號似乎沒有一個專門的稱謂,姑且稱之為“擇一匹配”吧。事實上,像[a-z]也是一種擇一匹配,只不過它只能匹配單個字符,而(|)則提供了更大的范圍,(ab|xy)表示匹配ab或匹配xy。注意“|”與“()”在此是一個整體。下面提供一些簡單的示例:
復(fù)制代碼 代碼如下:

string x = "0";
string y = "0.23";
string z = "100";
string a = "100.01";
string b = "9.9";
string c = "99.9";
string d = "99.";
string e = "00.1";
Regex r = new Regex(@"^\+?((100(.0+)*)|([1-9]?[0-9])(\.\d+)*)$");
Console.WriteLine("x match count:" + r.Matches(x).Count);//1
Console.WriteLine("y match count:" + r.Matches(y).Count);//1
Console.WriteLine("z match count:" + r.Matches(z).Count);//1
Console.WriteLine("a match count:" + r.Matches(a).Count);//0
Console.WriteLine("b match count:" + r.Matches(b).Count);//1
Console.WriteLine("c match count:" + r.Matches(c).Count);//1
Console.WriteLine("d match count:" + r.Matches(d).Count);//0
Console.WriteLine("e match count:" + r.Matches(e).Count);//0
//匹配0到100的數(shù)。最外層的括號內(nèi)包含兩部分“(100(.0+)*)”,“([1-9]?[0-9])(\.\d+)*”,這兩部分是“OR”的關(guān)系,即正則表達式引擎會先嘗試匹配100,如果失敗,則嘗試匹配后一個表達式(表示[0,100)范圍中的數(shù)字)。

(6)特殊字符的匹配
下面提供一些簡單的示例:

view plaincopy to clipboardprint?
string x = "\\";
Regex r1 = new Regex("^\\\\$");
Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1
Regex r2 = new Regex(@"^\\$");
Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1
Regex r3 = new Regex("^\\$");
Console.WriteLine("r3 match count:" + r3.Matches(x).Count);//0
//匹配“\”

string x = "\"";
Regex r1 = new Regex("^\"$");
Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1
Regex r2 = new Regex(@"^""$");
Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1
//匹配雙引號
string x = "\\";
Regex r1 = new Regex("^\\\\$");
Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1
Regex r2 = new Regex(@"^\\$");
Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1
Regex r3 = new Regex("^\\$");
Console.WriteLine("r3 match count:" + r3.Matches(x).Count);//0
//匹配“\”

string x = "\"";
Regex r1 = new Regex("^\"$");
Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1
Regex r2 = new Regex(@"^""$");
Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1
//匹配雙引號



(7)組與非捕獲組
以下提供一些簡單的示例:
復(fù)制代碼 代碼如下:

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)容,作為一個“組”,并且可以通過索引的方式進行引用。表達式中的“\1”,用于反向引用表達式中出現(xiàn)的第一個組,即粗體標(biāo)識的第一個括號內(nèi)容,“\2”則依此類推。

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
}
//獲取組中的內(nèi)容。注意,此處是Groups[1],因為Groups[0]是整個匹配的字符串,即整個變量x的內(nèi)容。

string x = "Live for nothing,die for something";
Regex r = new Regex(@"^Live for no(?<g1>[a-z]{5}),die for some\1$");
if (r.IsMatch(x))
{
Console.WriteLine("group1 value:" + r.Match(x).Groups["g1"].Value);//輸出:thing
}
//可根據(jù)組名進行索引。使用以下格式為標(biāo)識一個組的名稱(?<groupname>…)。

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
}
//刪除原字符串中重復(fù)出現(xiàn)的“nothing”。在表達式之外,使用“$1”來引用第一個組,下面則是通過組名來引用:
string x = "Live for nothing nothing";
Regex r = new Regex(@"(?<g1>[a-z]+) \1");
if (r.IsMatch(x))
{
x = r.Replace(x, "${g1}");
Console.WriteLine("var x:" + x);//輸出:Live for nothing
}

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);//輸出:(空)
}
//在組前加上“?:”表示這是個“非捕獲組”,即引擎將不保存該組的內(nèi)容。

相關(guān)文章

最新評論