6

In .NET Core and .NET Framework 4.x the following code works as expected:

var match = Regex.Match(src, pattern)
    .Groups
    .Cast<Group>()
    .Where(grp => grp.Name.StartsWith("val"));

However, in netstandard, the Name property in Group is gone. I'm wondering if there is a new way of achieving the same thing, or if this is a bug.


Edit: I first thought this was a netstandard 2.0 issue, but it looks like the property is missing from all netstandard versions.

Workaround for now: .Where(grp => ((string)((dynamic)grp).Name).StartsWith("val")), which is obviously less than ideal.

Athari
  • 33,702
  • 16
  • 105
  • 146
Andre Andersen
  • 1,211
  • 1
  • 11
  • 19
  • are you sure all of your groups are named? what if some groups not named? – Lei Yang Jul 03 '17 at 01:32
  • @LeiYang Well, it works just fine with .NET Core and .NET Framework... What's your point? – Andre Andersen Jul 03 '17 at 01:34
  • @LeiYang Yes, it runs without exceptions, and as one would expect. – Andre Andersen Jul 03 '17 at 02:59
  • [msdn](https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.group.name(v=vs.110).aspx) says this property is from .net 4.7, and i think .net standard is a minimal feature sets so it is reasonable not to have this property. – Lei Yang Jul 03 '17 at 03:05
  • 1
    Really? Heh... As stated, it also works in .NET Core... I don't see the reason for removing this property, as you can use the string indexer to access the named group - and additionally, both .NET Framework *AND* .NET Core has this property. I don't agree that it is "reasonable" not to have this feature. .net standard is NOT a "minimal feature sets". It's a standard contract. Please refrain from adding incorrect or non-constructive comments. – Andre Andersen Jul 03 '17 at 03:12
  • .net 4.5 doesn't have this property i just tried. – Lei Yang Jul 03 '17 at 03:12
  • That does not help me solve my problem. – Andre Andersen Jul 03 '17 at 03:15
  • i never need a group name property, i just use group index. and there is [GetGroupNames method](https://stackoverflow.com/questions/1381097/how-do-i-get-the-name-of-captured-groups-in-a-c-sharp-regex) – Lei Yang Jul 03 '17 at 03:19
  • @LeiYang Thank you for that information. Take a look at this screen shot: http://i.imgur.com/cKdfPUi.png - as you can see, there actually is a "Name" property in Group in .NET 4.5 as well, it's just that the compiler doesn't like it for some reason. – Andre Andersen Jul 03 '17 at 03:36
  • ok... wait for somebody else to answer – Lei Yang Jul 03 '17 at 04:07
  • for the workaround I had to cast it this way: matches.Groups.Cast().Where(g=>g.Name == "something") – Rafe Mar 04 '21 at 15:01

2 Answers2

8

According to its entry on apisof.net this property is only available on .NET Core 1.1 and .NET Framework 4.7 and upwards and has not been added to any version of .NET Standard. On other platforms (lower .NET versions, Xamarin, …) your workaround might throw an exception at runtime.

If you absolutely need to use this property in a library, I suggest multi-targeting to net47;netcoreapp1.1 instead of targeting a version of .NET Standard.

You are seeing the property in the debugger even if you target 4.5 because you are actually running on .NET 4.7 (because it is the version you have installed) and the debugger will show you everything that is available at runtime. The compiler however limits you the minimum version of .NET (Framework/Standard/…) you are targeting.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • Take a look at this screen shot: i.imgur.com/cKdfPUi.png - as you can see, there actually is a "Name" property in Group in .NET 4.5 as well, it's just that the compiler doesn't like it for some reason. – Andre Andersen Jul 03 '17 at 04:51
  • Yes because you are **running** on 4.7 and the debugger shows you what is available at runtime. The compiler limits you to the minimum version of .NET that you **target** to run on. – Martin Ullrich Jul 03 '17 at 04:53
  • Added it to the answer – Martin Ullrich Jul 03 '17 at 04:55
  • @AndreAndersen in your comments you **insist** saying 'I don't agree that it is "reasonable" not to have this feature', and this answer seems has not told you **why**, so why did you accept as answer? – Lei Yang Jul 03 '17 at 06:27
  • @LeiYang I accepted the answer because his first approach was helping me finding a way to continue working with the 'new' regex group property. He happened to additionally explain why I saw the property while debugging a .net 4.5 application, and realized "of course, that's why I see the property". I can't see how your statements are being backed by his answer though. – Andre Andersen Jul 03 '17 at 13:05
  • so now do you think it is reasonable **not** to have this property in .net standard? 'I don't see the reason for removing this property'---and can you see now? if you can, what's the reason? – Lei Yang Jul 03 '17 at 13:12
  • @LeiYang the "reason" is not part of the original question and I'd suggest opening an issue on github.com/dotnet/standard on reasons for including specific API surface. (though I can explain: intersection of .net 4.6.1 surface with added APIs from netstandard 1.6 to not cause breaking API surface changes even though they may throw at runtime. since `Name` isn't in std 1.6 or net461, it it is not yet in any released version of .NET Standard) – Martin Ullrich Jul 03 '17 at 13:24
  • so in my comments i said '.net standard is a minimal feature sets', and now what you said is about same meaning, am i wrong? the OP said what i said is 'incorrect or non-constructive comments' – Lei Yang Jul 03 '17 at 13:27
  • @LeiYang I never said I agreed with you, nor did I imply it. What I *did* realize was the reason for the property to show up with the .NET 4.5 targeting. That doesn't mean I agree with your statement. – Andre Andersen Jul 03 '17 at 14:36
  • i cannot agree with you either, in your question you said 'In .NET Core and .NET Framework 4.x the following code works as expected:', but now all of us know only above 4.7 can it compile. you're wrong from the beginning. by the way, i did not expect you to agree, my point is you cannot disagree my points either. – Lei Yang Jul 03 '17 at 14:41
2

Named groups could be accessed through Regex.GetGroupNames(), it works on .NET Standard either.

Useful sample found at Regex.GetGroupNames Method

AhmadYo
  • 334
  • 3
  • 6