1

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.

regex101 result screenshot

Adriano
  • 3,788
  • 5
  • 32
  • 53
Paul
  • 1,375
  • 2
  • 21
  • 55

1 Answers1

1

Try with this regex:

(?:.*\/(?<root>family\-resources|employer\-resources|newsroom|text\-pages))?\/(?:(?<path>[0-9A-z=\-\s]+\/?)+)

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.

RegEx

(?: ) 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:

(?<=\/)(?<root>family\-resources|employer\-resources|newsroom|text\-pages)?\/(?:(?<path>[0-9A-z=\-\s]+\/?)+)

This is slightly more advanced, but is closer to what you tried to achieve.

Guillaume F.
  • 5,905
  • 2
  • 31
  • 59