2

I work with JSON lists which can look somewhat like the following:

 [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$unauthenticated",
      "permission": "DENY"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    }
  ]

What would be the proper way to write a regex which correctly extracts only the list item which contains the word $everyone? I need to extract the entire object, so the correct result should be:

   {
     "accessType": "*",
     "principalType": "ROLE",
     "principalId": "$everyone",
     "permission": "DENY"
   }

I have tried something like \{(?s).*everyone(?s).*\}, but this will match the first and last opening and closing curly bracket in the list, with everything in between.

8
  • Have you tried .*? instead of .*? Commented Aug 25, 2015 at 9:38
  • Can't you parse the JSON instead? Concerning the regex, try with .*? instead of .*. Commented Aug 25, 2015 at 9:38
  • @sp00m I can't do it in code unfortunately - this regex needs to be plugged into the search feature of my IDE (IDEA) in order to perform multiline search-and-replace over a large number of files. IDEA unfortunately only supports normal multiline find-and-replace for single files. Commented Aug 25, 2015 at 9:41
  • \{(?s).*?everyone(?s).*?\} almost works, in the sense that it stops the search at the first } encountered after everyone is found. Now I only need it to also start at the first { before that. Commented Aug 25, 2015 at 9:43
  • 1
    If it's one-off task: \{[^}]*?everyone[^}]*\} Commented Aug 25, 2015 at 9:44

1 Answer 1

2

For a one-off task of search and replace in IDE:

\{[^}]*?everyone[^}]*\}

If you need the $, use either:

\{[^}]*?\$everyone[^}]*\}
\{[^}]*?[$]everyone[^}]*\}

Instead of (?s).* which allows anything in between { and everyone and between everyone and }, I restrict it to [^}]*?, which disallows } in the "free" part.

A lot of assumptions is made in the regex, so don't use it in general case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.