當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。