當前位置: 首頁>>代碼示例>>C#>>正文


C# Regex.Matches方法代碼示例

本文整理匯總了C#中System.Text.RegularExpressions.Regex.Matches方法的典型用法代碼示例。如果您正苦於以下問題:C# Regex.Matches方法的具體用法?C# Regex.Matches怎麽用?C# Regex.Matches使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Text.RegularExpressions.Regex的用法示例。


在下文中一共展示了Regex.Matches方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

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

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      Regex rgx = new Regex(pattern);
      string sentence = "Who writes these notes?";
      
      foreach (Match match in rgx.Matches(sentence))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:17,代碼來源:Regex.Matches

輸出:

Found 'writes' at position 4
Found 'notes' at position 17

示例2:

Match match = regex.Match(input);
while (match.Success) {
      // Handle match here...

      match = match.NextMatch();
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:6,代碼來源:Regex.Matches

示例3: Main

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

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      Regex rgx = new Regex(pattern);
      string sentence = "Who writes these notes and uses our paper?";
      
      // Get the first match.
      Match match = rgx.Match(sentence);
      if (match.Success) {
         Console.WriteLine("Found first 'es' in '{0}' at position {1}", 
                           match.Value, match.Index);
         // Get any additional matches.
         foreach (Match m in rgx.Matches(sentence, match.Index + match.Length))
            Console.WriteLine("Also found '{0}' at position {1}", 
                              m.Value, m.Index);
      }   
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:24,代碼來源:Regex.Matches

輸出:

Found first 'es' in 'writes' at position 4
Also found 'notes' at position 17
Also found 'uses' at position 27

示例4:

Match match = regex.Match(input, startAt);
while (match.Success) {
      // Handle match here...

      match = match.NextMatch();
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:6,代碼來源:Regex.Matches

示例5: Main

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

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      string sentence = "Who writes these notes?";
      
      foreach (Match match in Regex.Matches(sentence, pattern))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:16,代碼來源:Regex.Matches

輸出:

Found 'writes' at position 4
Found 'notes' at position 17

示例6:

Match match = Regex.Match(input, pattern);
while (match.Success) {
      // Handle match here...

      match = match.NextMatch();
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:6,代碼來源:Regex.Matches

示例7: Main

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

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      string sentence = "NOTES: Any notes or comments are optional.";
      
      // Call Matches method without specifying any options.
      foreach (Match match in Regex.Matches(sentence, pattern))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
      Console.WriteLine();

      // Call Matches method for case-insensitive matching.
      foreach (Match match in Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:23,代碼來源:Regex.Matches

輸出:

Found 'notes' at position 11

Found 'NOTES' at position 0
Found 'notes' at position 11

示例8:

Match match = Regex.Match(input, pattern, options);
while (match.Success) {
      // Handle match here...

      match = match.NextMatch();
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:6,代碼來源:Regex.Matches

示例9: Main

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

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      string sentence = "NOTES: Any notes or comments are optional.";
      
      // Call Matches method without specifying any options.
      try {
         foreach (Match match in Regex.Matches(sentence, pattern,
                                               RegexOptions.None,
                                               TimeSpan.FromSeconds(1)))
            Console.WriteLine("Found '{0}' at position {1}", 
                              match.Value, match.Index);
      }
      catch (RegexMatchTimeoutException) {
         // Do Nothing: Assume that timeout represents no match.
      }
      Console.WriteLine();

      // Call Matches method for case-insensitive matching.
      try { 
         foreach (Match match in Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase))
            Console.WriteLine("Found '{0}' at position {1}", 
                              match.Value, match.Index);
      }
      catch (RegexMatchTimeoutException) {}
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:33,代碼來源:Regex.Matches

輸出:

Found 'notes' at position 11

Found 'NOTES' at position 0
Found 'notes' at position 11

示例10: while

try {
      Match match = Regex.Match(input, pattern, options,
                                TimeSpan.FromSeconds(1));
      while (match.Success) {
            // Handle match here...

            match = match.NextMatch();
      }  
   }
   catch (RegexMatchTimeoutException) {
      // Do nothing: assume that exception represents no match.
   }
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:12,代碼來源:Regex.Matches


注:本文中的System.Text.RegularExpressions.Regex.Matches方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。