I have a terrible feeling this may reduce itself to a dummy-on-me, forest-for-trees situation, and if that's the case, mea culpa in advance. But for the life of me I'm just not understanding why the following line will not compile in C#, assuming myRegEx is a RegEx object and myString is the target for a call to the Match method, as follows:
String[] results = myRegEx.Matches(myString)[0].Groups["Group1"].Captures.Select(x => x.Value).ToArray<String>();
The .Captures reference should get me to the CaptureCollection, which implements IEnumerable, and IEnumerable offers an extension method Select for a transform as I've attempted here, snagging the Value property for each item in the collection and pushing it into a string array.
However, the compiler barks at me with
'System.Text.RegularExpressions.CaptureCollection does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type System.Text.RegularExpression.CaptureCollection' could be found.
I can overcome this by calling the .Cast<Capture>() method from the Captures object, and then call select with a transform that, in turn, accesses the Value property, but that seems a little silly considering the objects already are Capture objects.
What am I doing wrong? Many thanks in advance for pointing out what must be a painfully obvious oversight on my part.
You are not doing anything wrong. MatchCollection and CaptureCollection implement only IEnumerable interface not IEnumerable<T>
That is why you need Cast<T> or OfType<T>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With