在比赛之后或之前得到一个单词?
问题描述:
答
易与捕获组:(\w+)(?:\:)(\w+)
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(\w+)(?:\:)(\w+)";
string input = @"aa bb cc color:red dd ee ff";
foreach (Match m in Regex.Matches(input, pattern))
{
Console.WriteLine("'{0}' full math at index {1}.", m.Value, m.Index);
Console.WriteLine("Capture group 1: '{0}'", m.Groups[1].Value);
Console.WriteLine("Capture group 2: '{0}'", m.Groups[2].Value);
}
}
}
输出:
'color:red' full math at index 9.
Capture group 1: 'color'
Capture group 2: 'red'
PS:你不需要逃避字面结肠,我只是这样做是为了使图案更具有可读性。
+0
这正是我需要的!谢谢!顺便说一句,我只是想知道,在正则表达式中使用@有什么用? –
+0
这不是正则表达式。 @使它成为[逐字字符串](https://blogs.msdn.microsoft.com/csharpfaq/2004/03/12/what-does-an-before-the-start-of-a-string-literal-意思/) – wp78de
请分享你的努力! –