Question

I have a sitecore template field which I want to restrict to one or more or the following guids. They can appear in any order and will be pipe separated.

  • {7f05059e-4b4c-4d4d-bb39-1f57446719ef}
  • {57cb8e77-30cb-4dba-88bb-104cfea2e94f}
  • {c41ac6b6-a5c9-47f9-86c2-7db9e861e701}
  • {97133d89-36a1-4c28-9f30-4516eb469520}

So far I have this regex, but this needs to FAIL if any other text other than the above guids and the separating pipes is entered.

[\|]?\{(7f05059e-4b4c-4d4d-bb39-1f57446719ef|57cb8e77-30cb-4dba-88bb-104cfea2e94f|c41ac6b6-a5c9-47f9-86c2-7db9e861e701|97133d89-36a1-4c28-9f30-4516eb469520)\}

How can I get this working as described?

I'm using sitecore, so I assume we are using .net regex here but it is difficult to tell :\

Was it helpful?

Solution

You need a little bit of modification to your expression to get it to work:

^(?:\||(?:\{(?:7f05059e-4b4c-4d4d-bb39-1f57446719ef|57cb8e77-30cb-4dba-88bb-104cfea2e94f|c41ac6b6-a5c9-47f9-86c2-7db9e861e701|97133d89-36a1-4c28-9f30-4516eb469520)\}))*$

I have added the anchors ^ and $ to match the start-of-line and end-of-line respectively.

I have also added the * to entire grouping to support one or more of your GUIDs on the same line.

Regex101 Demo

OTHER TIPS

Try ^ (line start) and $ (line end)

How about this:

^\{(7f05059e-4b4c-4d4d-bb39-1f57446719ef|57cb8e77-30cb-4dba-88bb-104cfea2e94f|c41ac6b6-a5c9-47f9-86c2-7db9e861e701|97133d89-36a1-4c28-9f30-4516eb469520)\}([\|]?\{(7f05059e-4b4c-4d4d-bb39-1f57446719ef|57cb8e77-30cb-4dba-88bb-104cfea2e94f|c41ac6b6-a5c9-47f9-86c2-7db9e861e701|97133d89-36a1-4c28-9f30-4516eb469520)\})*$

Here I have added ^ and $ to match the start and end of the string. Also, I have repeated the wanted strings for the first time and put the other one into a group. Then that group is repeated 0 to infinite times.

^\([\|]?\{(7f05059e-4b4c-4d4d-bb39-1f57446719ef|57cb8e77-30cb-4dba-88bb-104cfea2e94f|c41ac6b6-a5c9-47f9-86c2-7db9e861e701|97133d89-36a1-4c28-9f30-4516eb469520)\})+$

This is a shorter version, but allows an extra pipe in the beginning of the string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top