C#使用正则表达式

下面就来研究C#中的正则表达式,C#中的正则表达式包含在.NET基础雷库的一个名称空间下,这个名称空间就是System.Text.RegularExpressions。该名称空间包括8个类,1个枚举,1个委托。他们分别是:
                     Capture: 包含一次匹配的结果; 
                     CaptureCollection: Capture的序列; 
                     Group: 一次组记录的结果,由Capture继承而来; 
                     GroupCollection:表示捕获组的集合
                     Match: 一次表达式的匹配结果,由Group继承而来; 
                     MatchCollection: Match的一个序列; 
                     MatchEvaluator: 执行替换操作时使用的委托; 
                     Regex:编译后的表达式的实例。 
                     RegexCompilationInfo:提供编译器用于将正则表达式编译为独立程序集的信息
                     RegexOptions 提供用于设置正则表达式的枚举值
Regex类中还包含一些静态的方法: 
                    Escape: 对字符串中的regex中的转义符进行转义; 
                    IsMatch: 如果表达式在字符串中匹配,该方法返回一个布尔值; 
                    Match: 返回Match的实例; 
                    Matches: 返回一系列的Match的方法; 
                    Replace: 用替换字符串替换匹配的表达式; 
                    Split: 返回一系列由表达式决定的字符串; 
                    Unescape:不对字符串中的转义字符转义。
首先从使用Regex、Match类的简单表达式开始学习:

 

ContractedBlock.gifExpandedBlockStart.gifCode
            Regex emailregex = new Regex("(?<user>[^@]+)@(?<host>.+)");
            String s 
= "johndoe@tempuri.org";

            Match m 
= emailregex.Match(s);

            
if ( m.Success ) 
            {
                System.Console.WriteLine(
"User: " + m.Groups["user"].Value);
                System.Console.WriteLine(
"Host: " + m.Groups["host"].Value);
            } 
            
else 
            {
                System.Console.WriteLine(s 
+ " is not a valid email address");
            }

IsMatch方法指示 Regex 构造函数中指定的正则表达式在输入字符串中是否找到匹配项。这是我们使用C#正则表达式时最常用的方法之一。下面的例子说明了IsMatch方法的使用:

ContractedBlock.gifExpandedBlockStart.gifCode
            Regex emailregex = new Regex("(?<user>[^@]+)@(?<host>.+)");
            
if(emailregex.IsMatch("ddd*.com"))
            {
                System.Console.WriteLine(
"Matched successfully");
            }
            
else
            {
                System.Console.WriteLine(
"Matched failed");
            }
            System.Console.ReadLine();

Split方法是把由正则表达式匹配项定义的位置将输入字符串拆分为一个子字符串数组。例如:

 

ContractedBlock.gifExpandedBlockStart.gifCode
Regex r = new Regex("-"); // Split on hyphens.
string[] s = r.Split("first-second-third");
for(int i=0;i<s.Length;i++)
{
 Response.Write(s[i]
+"<br>");
}

 

执行的结果是:
First
Second
Third

看上去和String的Split方法一样,但string的Split方法在由正则表达式而不是一组字符确定的分隔符处拆分字符串。

Match方法是在输入字符串中搜索正则表达式的匹配项,并Regex 类的 Match 方法返回 Match 对象,Match 类表示正则表达式匹配操作的结果。下面的例子演示Match方法的使用,并利用Match对象的Group属性返回Group对象:

ContractedBlock.gifExpandedBlockStart.gifCode
string text = @"public string testMatchObj string s string  match ";
string pat = @"(\w+)\s+(string)";
// Compile the regular expression.
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
// Match the regular expression pattern against a text string.
Match m = r.Match(text);
int matchCount = 0;
while (m.Success) 
{
 Response.Write(
"Match"+ (++matchCount) + "<br>");
 
for (int i = 1; i <= 2; i++
 {
  Group g 
= m.Groups[i];
  Response.Write(
"Group"+i+"='" + g + "'"  + "<br>");
  CaptureCollection cc 
= g.Captures;
  
for (int j = 0; j < cc.Count; j++
  {
   Capture c 
= cc[j];
   Response.Write(
"Capture"+j+"='" + c + "', Position="+c.Index + "<br>");
  }
 }
 m 
= m.NextMatch();
}

该事例运行结果是:
Match1
Group1='public'
Capture0='public', Position=0
Group2='string'
Capture0='string', Position=7
Match2
Group1='testMatchObj'
Capture0='testMatchObj', Position=14
Group2='string'
Capture0='string', Position=27
Match3
Group1='s'
Capture0='s', Position=34
Group2='string'
Capture0='string', Position=36

转载于:https://www.cnblogs.com/qiantuwuliang/archive/2009/05/30/1492368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值