当前位置: 首页>>代码示例>>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;未经允许,请勿转载。