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.