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