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类的简单表达式开始学习:
 
 
 
Code
            Regex emailregex = new Regex("(?[^@]+)@(?.+)");
            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方法的使用:
 
 
Code
            Regex emailregex = new Regex("(?[^@]+)@(?.+)");
            if(emailregex.IsMatch("ddd*.com"))
            {
                System.Console.WriteLine("Matched successfully");
            }
            else
            {
                System.Console.WriteLine("Matched failed");
            }
            System.Console.ReadLine();
Split方法是把由正则表达式匹配项定义的位置将输入字符串拆分为一个子字符串数组。例如:
 
 
 
Code
Regex r = new Regex("-"); // Split on hyphens.
string[] s = r.Split("first-second-third");
for(int i=0;is.Length;i++)
{
Response.Write(s+"
");
}
 
执行的结果是:
First
Second
Third
看上去和String的Split方法一样,但string的Split方法在由正则表达式而不是一组字符确定的分隔符处拆分字符串。
Match方法是在输入字符串中搜索正则表达式的匹配项,并Regex 类的 Match 方法返回 Match 对象,Match 类表示正则表达式匹配操作的结果。下面的例子演示Match方法的使用,并利用Match对象的Group属性返回Group对象:
 
 
Code
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) + "
");
for (int i = 1; i  2; i++) 
{
  Group g = m.Groups;
  Response.Write("Group"+i+"='" + g + "'"  + "
");
  CaptureCollection cc = g.Captures;
  for (int j = 0; j  cc.Count; j++) 
  {
   Capture c = cc[j];
   Response.Write("Capture"+j+"='" + c + "', Position="+c.Index + "
");
  }
}
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
分享到:
QQ空间
腾讯微博
腾讯朋友