正则表达式在.Net中的使用(C#)

<script type="text/javascript">function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>  

.Net 框架内的三个主要处理正则表达式的类RegEx、Match和MatchCollection,这三个类都可以在System.Text.RegularExpressions命名空间下找到

==============================================================================

1.调用RegEx的IsMatch方法检查字符串是否与正则表达式匹配

       Regex objRegex;

       objRegex = new Regex( "正则表达式字符串" );

       bool result = objRegex.IsMatch( "需要匹配的字符串");

eg.        输出结果应为true,表匹配成功

Regex objRegex = new Regex("sophi[ae]" );

      Response.Write(objRegex.IsMatch(
"sophia"));

 

==============================================================================

2.作为替换,也可以使用RegEx类的实例的Replace方法来匹配和替换字符串的一部分。

       Regex objRegEx = new Regex("正则表达式字符串");

       替换后的字符串 = objRegEx.Replace("需要替换的原字符串","用作替换的字符串");

eg.         输出结果为:sophi? is curious ?bout ?ll int?r?sting things.

Regex objRegEx = new Regex ("[ae]" );

      
string strOrigin = "Sophia is curious about all interesting things."


       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

 RegEx objRegEx = new RegEx("eaw+" );

       Match objMatch 
= objRegEx.Match("The mothod is easy."
);

      Response.Write(
"Success:" + objMatch.Success+"<BR>"
);

      Response.Write(
"Index:" + objMatch.Index+"<BR>"
);

      Response.Write(
"Length:" + objMatch.Length+"<BR>"
);

      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.      

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



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=975223

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值