對(duì)C#中正則表達(dá)式的一些解讀和總結(jié)(4)_.Net教程
推薦:從Internet上抓取指定URL的源碼的方案(C#)引言: 在做無(wú)線(xiàn)項(xiàng)目的時(shí)候,與通訊公司的數(shù)據(jù)通訊有一部分是通過(guò)XML交互的,所以必須要?jiǎng)討B(tài)抓取通訊公司提供的固定的Internet上的數(shù)據(jù),便研究了一下如何抓取固定url上的數(shù)據(jù),現(xiàn)與
string text = "the quick red fox jumped over the lazy brown dog.";
System.Console.WriteLine("text=[" text "]");
string result = "";
string pattern = @"\w |\W ";
foreach (Match m in Regex.Matches(text, pattern))
{
// 取得匹配的字符串
string x = m.ToString();
// 如果第一個(gè)字符是小寫(xiě)
if (char.IsLower(x[0]))
// 變成大寫(xiě)
x = char.ToUpper(x[0]) x.Substring(1, x.Length-1);
// 收集所有的字符
result = x;
}
System.Console.WriteLine("result=[" result "]");
正象上面的例子所示,我們使用了C#語(yǔ)言中的foreach語(yǔ)句處理每個(gè)匹配的字符,并完成相應(yīng)的處理,在這個(gè)例子中,新創(chuàng)建了一個(gè)result字符串。這個(gè)例子的輸出所下所示:
text=[the quick red fox jumped over the lazy brown dog.]
result=[The Quick Red Fox Jumped Over The Lazy Brown Dog.]
基于表達(dá)式的模式
完成上例中的功能的另一條途徑是通過(guò)一個(gè)MatchEvaluator,新的代碼如下所示:
static string CapText(Match m){
//取得匹配的字符串
string x = m.ToString();
// 如果第一個(gè)字符是小寫(xiě)
if (char.IsLower(x[0]))
// 轉(zhuǎn)換為大寫(xiě)
return char.ToUpper(x[0]) x.Substring(1, x.Length-1);
return x;
}
static void Main(){
string text = "the quick red fox jumped over the
lazy brown dog.";
System.Console.WriteLine("text=[" text "]");
string pattern = @"\w ";
string result = Regex.Replace(text, pattern,
new MatchEvaluator(Test.CapText));
System.Console.WriteLine("result=[" result "]");
}
分享:ASP.NET對(duì)IIS中的虛擬目錄進(jìn)行操作//假如虛擬目錄名為"Webtest",先在項(xiàng)目中引用 //System.DirectoryServices.dll,再 using System.DirectoryServices; protected System.DirectoryServices.DirectoryEntry di
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶(hù)端IP常見(jiàn)代碼存在的偽造IP問(wèn)題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(kù)(連接字符串的配置及獲取)
- asp.net頁(yè)面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)示例代碼
- 相關(guān)鏈接:
- 教程說(shuō)明:
.Net教程-對(duì)C#中正則表達(dá)式的一些解讀和總結(jié)(4)
。