发布:2022/12/19 17:21:12作者:管理员 来源:本站 浏览次数:545
在下文中一共展示了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);
}
}
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4