I have the following Regex:
(?:\/(?<root>family-resources|employer-resources|newsroom|text-pages))?\/(?:(?<path>[0-9A-z=\-\s]+\/?)+)
As you can see, I'm trying to set up two named capture groups - root and path. However, when running this and checking the match groups, there is no root group - only path.
Using C#:
root = match.Groups["root"]?.Value ?? "Text Pages"; // Returns an empty string as the root group is missing.
I was able to reproduce this by running the Regex pattern on https://regex101.com/.
If you put in the following test string:
/sitecore/content/Corporate-New/home/employer-resources/back-up-care
You'll notice you only get one named capture group back - path.
Why is root not being returned?
It definitely seems like I'm using the right syntax for a named capture group. I've tried adding parentheses both outside the capture group and inside the regex pattern, and neither worked. I was thinking maybe the Regex pattern wasn't being understood.

Try with this regex:
I added an anti-slash in front of each
-since they are reserved, and a.*at the beginning to accept any path before your first group.(?: )requires everything between the parenthesis to be in the non-captured group. The.*allows the beginning of the path to match the pattern. Otherwise, the first slash encountered will match and stop the parsing (at sitecore).Another solution would be to use a look-behind:
This is slightly more advanced, but is closer to what you tried to achieve.