Mateusz Urbański
Mateusz Urbański

Reputation: 7862

Rewrite Regex to support both scenarios

I have the following regex to get all values with secure prefix:

"secure[^:]+:\s*"[^"]+"

It works fine for the following string:

Parameters: {"client_id":"6dd930eb-e4dc-48d9-a18b-ace0c7406dc8","secure_client_secret":"fedfc0a7-af1e-413c-ab51-1955113b2123"}

but it does not work for the following string:

"Parameters: {\"client_id\":\"6dd930eb-e4dc-48d9-a18b-ace0c7406dc8\",\"secure_client_secret\":\"fedfc0a7-af1e-413c-ab51-1955113b2123\"}"

How can I update this to work for both scenarios? Here is link to rubular: https://rubular.com/r/ZA0ioy8mHXOghq

Upvotes: 3

Views: 34

Answers (1)

anubhava
anubhava

Reputation: 784898

You may use a capture use and back-reference to match " and \" wrapped values:

(\\"|")secure[^:]+:\s*\1.*?\1

Updated RegEx Demo

However do note that if your platform/tool allows it then use a proper JSON parser to parse a JSON string like this.

Upvotes: 4

Related Questions