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