如何从c#的捕获函数中捕获值

本文关键字:函数 | 更新日期: 2023-09-27 17:50:48

当我想在c#中使用Capture函数捕获值时,我遇到了一个问题。我的代码在字符串中寻找许多模式,所以我使用匹配集合,然后对于每个匹配,我使用Capture函数。但是当我想更换captureOut.value时,它不起作用。

我代码:

MatchCollection matches = Regex.Matches(string, @"'d*'.*'d+'s")
foreach (Match matchOut in matches)
{
    foreach (Capture captureOut in matchOut.Captures)
    Match match1 = Regex.Match(captureOut.Value, @"'d*'.*'d+");
::::: //}
     output = Regex.Replace(output,captureOut.Value, Function1);
}
// i change the value of pattern based on the output of function 1

这部分我的代码,我不知道为什么capture out.value不工作

如何从c#的捕获函数中捕获值

使用capture属性只有在你的正则表达式中有组时才有意义,即你的正则表达式的部分包含在()中。由于您的正则表达式没有,因此只有一个捕获组,并且它是与正则表达式匹配的整个字符串。