本文整理汇总了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);
}
}
输出:
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();
}
示例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);
}
}
}
输出:
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();
}
示例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);
}
}
输出:
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();
}
示例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);
}
}
输出:
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();
}
示例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) {}
}
}
输出:
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.
}