当前位置: 首页>>代码示例>>C#>>正文


C# Regex.Match方法代码示例

本文整理汇总了C#中System.Text.RegularExpressions.Regex.Match方法的典型用法代码示例。如果您正苦于以下问题:C# Regex.Match方法的具体用法?C# Regex.Match怎么用?C# Regex.Match使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Text.RegularExpressions.Regex的用法示例。


在下文中一共展示了Regex.Match方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\ba\w*\b";
      string input = "An extraordinary day dawns with each new day.";
      Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
      if (m.Success)
         Console.WriteLine("Found '{0}' at position {1}.", m.Value, m.Index);
   }
}
开发者ID:.NET开发者,项目名称:System.Text.RegularExpressions,代码行数:15,代码来源:Regex.Match

输出:

Found 'An' at position 0.

示例2: Main

//引入命名空间
using System;
using System.Text.RegularExpressions;

class Example 
{
   static void Main() 
   {
      string text = "One car red car blue car";
      string pat = @"(\w+)\s+(car)";

      // Instantiate the regular expression object.
      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) 
      {
         Console.WriteLine("Match"+ (++matchCount));
         for (int i = 1; i <= 2; i++) 
         {
            Group g = m.Groups[i];
            Console.WriteLine("Group"+i+"='" + g + "'");
            CaptureCollection cc = g.Captures;
            for (int j = 0; j < cc.Count; j++) 
            {
               Capture c = cc[j];
               System.Console.WriteLine("Capture"+j+"='" + c + "', Position="+c.Index);
            }
         }
         m = m.NextMatch();
      }
   }
}
开发者ID:.NET开发者,项目名称:System.Text.RegularExpressions,代码行数:35,代码来源:Regex.Match

输出:

Match1
Group1='One'
Capture0='One', Position=0
Group2='car'
Capture0='car', Position=4
Match2
Group1='red'
Capture0='red', Position=8
Group2='car'
Capture0='car', Position=12
Match3
Group1='blue'
Capture0='blue', Position=16
Group2='car'
Capture0='car', Position=21

示例3: Main

//引入命名空间
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "ablaze beagle choral dozen elementary fanatic " +
                     "glaze hunger inept jazz kitchen lemon minus " +
                     "night optical pizza quiz restoration stamina " +
                     "train unrest vertical whiz xray yellow zealous";
      string pattern = @"\b\w*z+\w*\b";
      Match m = Regex.Match(input, pattern);
      while (m.Success) {
         Console.WriteLine("'{0}' found at position {1}", m.Value, m.Index);
         m = m.NextMatch();
      }   
   }
}
开发者ID:.NET开发者,项目名称:System.Text.RegularExpressions,代码行数:20,代码来源:Regex.Match

输出:

'ablaze' found at position 0
'dozen' found at position 21
'glaze' found at position 46
'jazz' found at position 65
'pizza' found at position 104
'quiz' found at position 110
'whiz' found at position 157
'zealous' found at position 174

示例4: Regex.Match(String value,String pattern).Success

//引入命名空间
using System;
using System.Text.RegularExpressions;

class RegexSubstitution
{
   public static void Main()
   {

      string text = "Name";
      if ( !Regex.Match(text,"^[A-Z][a-zA-Z]*$" ).Success )
      {
         Console.WriteLine( "Invalid last name");
      }

   }
}
开发者ID:C#程序员,项目名称:System.Text.RegularExpressions,代码行数:17,代码来源:Regex.Match

示例5: Main

//引入命名空间
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;

public class MainClass{

   public static void Main(){
        Regex r1 = new Regex(@"((abc)*)x(\1)");
        string s1 = "abcabcabcxabc";
        Console.WriteLine(r1.IsMatch(s1) + "..." + r1.Match(s1).Value);
   }
}
开发者ID:C#程序员,项目名称:System.Text.RegularExpressions,代码行数:16,代码来源:Regex.Match


注:本文中的System.Text.RegularExpressions.Regex.Match方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。