1、贪婪与非贪婪
2、正向预搜索和反向预搜索
四、正则的几种常见用法
1、验证控件 RegularExpressionValidator
注意 1、RegularExpressionValidator不能验证非空,验证非空用RequiredFieldValidator,名司其职
2、如无特殊情况,用于验证控件的正则前后分别要加“^”和“$”,因为用验证控件来验证的内容是要完全匹配的,而不是成功匹配其中一部分即可通过验证
3、用于验证控件的正则,最后都要转换为客户端脚本,而由于脚本语言对正则的支持较弱,有些在cs程序里可以通过的正则,在验证控件里可能失效
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("不符合");
}
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 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";
}
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
4、替换
替换就是将一种形式的字符串替换为另一种形式,或是删除不需要的内容
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">
string resultStr = Regex.Replace(test, @" \[img\]([^\]]*?)\[/img\] " , @" <img src=""$1""> " );
举例 取出<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();
输出为:显示屏类型
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";
}
输出为:
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";
}
输出为:
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
有助于理解这一概念的帖子 http://community.csdn.net/Expert/topic/5520/5520530.xml?temp=.769314
http://community.csdn.net/Expert/topic/5533/5533722.xml?temp=.3884394
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>
{
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");
}
有助于理解这一概念的帖子 http://community.csdn.net/Expert/topic/5520/5520530.xml?temp=.769314
http://community.csdn.net/Expert/topic/5533/5533722.xml?temp=.3884394
文章来源: http://www.cnblogs.com/suiqirui19872005/archive/2007/06/08/777093.html