sara white
sara white

Reputation: 101

How can I capture value from capture function in C#

I have a problem when I want to capture the value using Capture function in C#. My code looks for many patterns in a string, so I use match collection, then for each match I use Capture function. But when I want to replace the captureOut.value it does not work.

My code:

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

This part of my code, I do not know why capture out.value does not work.

Upvotes: 0

Views: 139

Answers (1)

Petar Ivanov
Petar Ivanov

Reputation: 93020

Using the capture property only makes sense if you have groups in your regex, i.e. parts of your regex enclosed in ( ). Since your regex has no, there is only one captured group and it's the whole string that matches the regex.

Upvotes: 1

Related Questions