I had a regex, like so:
(?<one-1>cat)|(?<two-2>dog)|(?<three-3>mouse)|(?<four-4>fish)
When I tried to use this pattern in a .Net app, it failed, because the group name contained a ‘-‘ in it.
So, as a workaround, I tried to use two regexes, the first:
(?<A>cat)|(?<Be>dog)|(?<C>mouse)|(?<D>fish)
would match the original cases I was looking for into group names I could control.
And then, I intended to use the correctly matched group name from that regex in one like this:
(?<A>one-1)|(?<Be>two-2)|(?<C>three-3)|(?<D>four-4)
I would do so, by finding the string that matched this pattern and determining if the group names were equal.
I know this seems a bit convoluted. Thanks of any help offered.
Something along the lines of the following?
Output
First we build up a
Regexinstance by escaping the group names and combining them with the patterns. Any non-word character is replaced with the sequence_nnn_where nnn is its UTF-32 value.For matches, the .NET library doesn’t give us an easy way to get a group’s name, so we have to go the other way: for each group name, we check whether that group matched and if so unescape its name and let the caller know both name and captured substring.