正则表达式在.Net中的使用(C#)
2006-09-05 17:44
453 查看
Net 框架内的三个主要处理正则表达式的类RegEx、Match和MatchCollection,这三个类都可以在System.Text.RegularExpressions命名空间下找到
==============================================================================
1.调用RegEx的IsMatch方法检查字符串是否与正则表达式匹配
Regex objRegex;
objRegex = new Regex( "正则表达式字符串" );
bool result = objRegex.IsMatch( "需要匹配的字符串");
eg. 输出结果应为true,表匹配成功
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Regex objRegex = new Regex("sophi[ae]");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write(objRegex.IsMatch("sophia"));
==============================================================================
2.作为替换,也可以使用RegEx类的实例的Replace方法来匹配和替换字符串的一部分。
Regex objRegEx = new Regex("正则表达式字符串");
替换后的字符串 = objRegEx.Replace("需要替换的原字符串","用作替换的字符串");
eg. 输出结果为:sophi? is curious ?bout ?ll int?r?sting things.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Regex objRegEx = new Regex ("[ae]");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string strOrigin = "Sophia is curious about all interesting things."
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write(objRegEx.Replace(strOrigin,"?");
==============================================================================
3.如果要添加由RegEx类所匹配的表达式的附加信息,那么就可以返回一个Match类的实例。
RegEx objRegEx = new RegEx("正则表达式字符串");
Match objMatch = objRegEx.Match("需要匹配的字符串");
objMatch.Success //类型bool 表示是否匹配成功
objMatch.Index //类型int 表示从字符串的第几个字符开始匹配(第一个字符的Index为0)
objMatch.Length //类型int 表示所匹配字符串的长度
objMatch.Value //类型string 表示所匹配字符串的值
eg. 输出结果为:Success:true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RegEx objRegEx = new RegEx("eaw+");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Match objMatch = objRegEx.Match("The mothod is easy.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write("Success:" + objMatch.Success+"<BR>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write("Index:" + objMatch.Index+"<BR>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write("Length:" + objMatch.Length+"<BR>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write("Value:" + objMatch.Value+"<BR>");
Index: 14
Length:4
Value:easy
==============================================================================
4.正则表达式可以匹配字符串的多个地方。如果找到多个匹配,可用MatchCollection来表示所有匹配。
RegEx objRegEx = new RegEx("[正则表达式字符串");
MatchCollection objMatchCollection = objRegEx.Match("需要匹配的字符串");
foreach(Match objMatch in objMatchCollection)
Response.Write("Value:"+objMatch.Value+" ")
eg.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RegEx objRegEx = new RegEx("[aeiou]")
MatchCollection objMatchCollection
Response.Write("Value:"+objMatch.Value+" ")
foreach(Match objMatch in objMatchCollection)= objRegEx.Match("Do you understand?");
输出结果:Value:Do Value:you Value:understand
==============================================================================
1.调用RegEx的IsMatch方法检查字符串是否与正则表达式匹配
Regex objRegex;
objRegex = new Regex( "正则表达式字符串" );
bool result = objRegex.IsMatch( "需要匹配的字符串");
eg. 输出结果应为true,表匹配成功
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Regex objRegex = new Regex("sophi[ae]");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write(objRegex.IsMatch("sophia"));
==============================================================================
2.作为替换,也可以使用RegEx类的实例的Replace方法来匹配和替换字符串的一部分。
Regex objRegEx = new Regex("正则表达式字符串");
替换后的字符串 = objRegEx.Replace("需要替换的原字符串","用作替换的字符串");
eg. 输出结果为:sophi? is curious ?bout ?ll int?r?sting things.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Regex objRegEx = new Regex ("[ae]");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string strOrigin = "Sophia is curious about all interesting things."
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write(objRegEx.Replace(strOrigin,"?");
==============================================================================
3.如果要添加由RegEx类所匹配的表达式的附加信息,那么就可以返回一个Match类的实例。
RegEx objRegEx = new RegEx("正则表达式字符串");
Match objMatch = objRegEx.Match("需要匹配的字符串");
objMatch.Success //类型bool 表示是否匹配成功
objMatch.Index //类型int 表示从字符串的第几个字符开始匹配(第一个字符的Index为0)
objMatch.Length //类型int 表示所匹配字符串的长度
objMatch.Value //类型string 表示所匹配字符串的值
eg. 输出结果为:Success:true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RegEx objRegEx = new RegEx("eaw+");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Match objMatch = objRegEx.Match("The mothod is easy.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write("Success:" + objMatch.Success+"<BR>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write("Index:" + objMatch.Index+"<BR>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write("Length:" + objMatch.Length+"<BR>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Response.Write("Value:" + objMatch.Value+"<BR>");
Index: 14
Length:4
Value:easy
==============================================================================
4.正则表达式可以匹配字符串的多个地方。如果找到多个匹配,可用MatchCollection来表示所有匹配。
RegEx objRegEx = new RegEx("[正则表达式字符串");
MatchCollection objMatchCollection = objRegEx.Match("需要匹配的字符串");
foreach(Match objMatch in objMatchCollection)
Response.Write("Value:"+objMatch.Value+" ")
eg.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RegEx objRegEx = new RegEx("[aeiou]")
MatchCollection objMatchCollection
Response.Write("Value:"+objMatch.Value+" ")
foreach(Match objMatch in objMatchCollection)= objRegEx.Match("Do you understand?");
输出结果:Value:Do Value:you Value:understand
相关文章推荐
- 正则表达式在.Net中的使用(C#)
- C#中使用正则表达式验证电话号码、手机号、身份证号(转)
- C# 正则表达式的使用
- C#正则表达式的使用(Regex类用法)
- C#使用正则表达式验证电话号码、手机号、身份证号代码
- 通过代码学习C#&.NET——委托使用(正则表达式替换)
- C#中正则表达式使用介绍
- 正则表达式语法规则及在Javascript和C#中的使用方法
- C#正则表达式开源工具,为.net开源绵尽薄力
- C#正则表达式使用介绍
- C#中的正则表达式使用
- C#判断IP地址是否合法函数-使用正则表达式-2个
- C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编验证电话号码的主要代码如下:
- c#使用正则表达式
- C# 正则表达式 使用介绍
- c#正则表达式使用详解
- C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编
- .net验证控件的属性与使用技巧---总结及正则表达式语法
- C# 中使用正则表达式验证电话号码、手机号、身份证号
- C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编