三、正則中幾個痛點
1、貪婪與非貪婪
有助於理解這一概念的文章
http://community.csdn.net/Expert/topic/5435/5435240.xml?temp=.7199671
注意
在談到貪婪與非貪婪時,有一點大家可能會忽略,那就是單獨的一個“?”,表示的是貪婪模式,而“??”才是非貪婪模式。
舉例:
源字串:12
Regex(一):[1-9]/d?
Regex(二):[1-9]/d??
結果:(一)匹配成功,匹配結果為12,(二)匹配成功,匹配結果為1
2、正向預搜尋和反向預搜尋
有助於理解這一概念的文章
http://community.csdn.net/Expert/topic/5410/5410564.xml?temp=.1138422
四、正則的幾種常見用法1、驗證控制項 RegularExpressionValidator
注意
1、RegularExpressionValidator不能驗證非空,驗證非空用RequiredFieldValidator,名司其職
2、如無特殊情況,用於驗證控制項的正則前後分別要加“^”和“$”,因為用驗證控制項來驗證的內容是要完全符合的,而不是成功匹配其中一部分即可通過驗證 3、用於驗證控制項的正則,最後都要轉換為用戶端指令碼,而由於指令碼語言對正則的支援較弱,有些在cs程式裡可以通過的正則,在驗證控制項裡可能失效 有助於理解這一概念的文章
http://community.csdn.net/Expert/topic/5445/5445334.xml?temp=.3749658
2、匹配
匹配就是檢查字串是否滿足某一規律,或是否包含符合某一規律的子字串
Regex.IsMatch
舉例
判斷textBox1輸入內容是否為yyyy-MM格式
string yourStr = textBox1.Text;
if(Regex.IsMatch(yourStr, @"^/d{4}-(0/d|1[0-2])$"))
{
MessageBox.Show("符合");
}
else
{
MessageBox.Show("不符合");
}
3、提取
提取就是從字串中取出符合某一規律的子字串 Match m = Regex.Match MatchCollection mc = Regex.Matches 前者用來取出單一匹配結果,後者用來取出多個匹配結果,並存入集合
舉例
取出單個圖片地址
string yourStr = "<img src=/"http://www.ahelp.cn/bbs/UploadFile/2006-8/200681516161698723.jpg/" border=/"0/" alt=/"/"/>";
string resultStr = "";
Match m = Regex.Match(yourStr, @"<img/s[^>]*?src=([""']?)(?<source>[^""'/s]*)/1[^>]*?>", RegexOptions.IgnoreCase);
if (m.Success)
{
resultStr = m.Groups["source"].Value;
}
取出多個圖片地址
string yourStr = "<p><img src=/"http://www.ahelp.cn/bbs/UploadFile/2006-8/200681516161698723.jpg/" border=/"0/" alt=/"/"/></p><p> </p><p><img src=/"http://www.ahelp.cn/bbs/UploadFile/2006-8/200681516194447448.jpg/" border=/"0/" alt=/"/"/></p><p> </p><img src=/"http://photo.bababian.com/20060803/C97D6403CD4F2F85A5FBB57D5A791F5B_500.jpg/" border=/"0/" alt=/"/"/>";
MatchCollection mc = Regex.Matches(yourStr, @"<img/s[^>]*?src=([""']?)(?<src>[^""'/s]*)/1[^>]*?>", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["src"].Value + "/n";
}有助於理解這一概念的文章
http://community.csdn.net/Expert/topic/5479/5479400.xml?temp=.2616083
http://community.csdn.net/Expert/topic/5441/5441044.xml?temp=.34899544、替換
替換就是將一種形式的字串替換為另一種形式,或是刪除不需要的內容 Regex.Replace
舉例
將UBB代碼轉換為html格式
string test = "[img]http://www.csdn.net/logo.jpg[/img]";
string resultStr = Regex.Replace(test, @"/[img/]([^/]]*?)/[/img/]", @"<img src=""$1"">");
輸出為:<img src="http://www.csdn.net/logo.jpg">
舉例
取出<td>...</td>標籤內非html代碼部分
string test = "<TD CLASS=btd WIDTH=198 BGCOLOR=/"#FCFCFC/"><B> <a href=/"http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=3142/" target=/"_blank/">顯示屏類型</a></B></TD>";
string resultStr = Regex.Replace(test, @"<[^>]*?>","").Trim();
輸出為:顯示屏類型
5、分割
Regex.Split注意
在Split中使用擷取的群組,分割後的數組也包含擷取的群組的內容,即使使用零寬度擷取的群組,仍然會將擷取的群組的內容儲存到結果數組內
舉例
不存在擷取的群組時
string test = "aa<bbb>cc<ddd>ee";
string[] temp = Regex.Split(test, @"<[^>]*?>");
foreach (string s in temp)
{
richTextBox2.Text += s + "/n";
}
輸出為:
aa
cc
ee
存在擷取的群組時
string test = "aa<bbb>cc<ddd>ee";
string[] temp = Regex.Split(test, @"(<[^>]*?>)");
foreach (string s in temp)
{
richTextBox2.Text += s + "/n";
}
輸出為:
aa
<bbb>
cc
<ddd>
ee
有助於理解這一概念的文章
http://community.csdn.net/Expert/topic/5436/5436187.xml?temp=.7995264
6、委託
正則中的委託,有時候可以很優雅的解決某一類問題,或是提高效率 委託一般用在對符合某一條件的子字串進行處理,而不是處理所有
MatchEvaluator(string (Match) target)
舉例
只替換<B>...</B>標籤內不包含html代碼的部分為CRT
private void button2_Click(object sender, EventArgs e)
{
string yourStr = "<TD CLASS=btd WIDTH=198 BGCOLOR=/"#FCFCFC/"><B> <a href=/"http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=3142/" target=/"_blank/">顯示屏類型</a></B> <B> WUXGA+</B></TD>";
string resultStr = Regex.Replace(yourStr, @"(?<=<b[^>]*>)[^<>]*(?=</b>)", new MatchEvaluator(expReplace), RegexOptions.IgnoreCase);
}
private string expReplace(Match m)
{
return m.Value.Replace(m.Value, "CRT");
}
輸出為:<TD CLASS=btd WIDTH=198 BGCOLOR="#FCFCFC"><B> <a href="http://dict.pconline.com.cn/dic/sort.jsp?kindId=-1&dicId=3142" target="_blank">顯示屏類型</a></B> <B>CRT</B></TD>
有助於理解這一概念的文章
http://community.csdn.net/Expert/topic/5520/5520530.xml?temp=.769314
http://community.csdn.net/Expert/topic/5580/5580796.xml?temp=.6849176
http://community.csdn.net/Expert/topic/5578/5578425.xml?temp=.2284815