我目前的代码是:
const string st = "one two";
const string reg = "((?<first>one ))?(?=(?<second>two))";
var result = Regex.Matches(st, reg, RegexOptions.IgnoreCase);
Console.WriteLn(result.Count);
foreach (Match match in result)
{
Console.WriteLine("first={0}, second={1}", match.Groups["first"], match.Groups["second"]);
}
输出是:
2
first=one , second=two
first=, second=two
返回两个匹配项。
如何编写正则表达式以仅返回一个匹配项?