I am trying to write code to extract some values from string following some pattern. Basically a the key and value from json where value is enclosed in {{value}}
In the below code:
String jsonString = "{\"title\":\"{{string_64|dsm_title}}\",\"code\":\"{{string_16|dsm_code}}\",\"enabled\":true,\"enableAdvanceFilter\":false,\"deleteAfterScheduledTime\":false,\"enableDataStoreLog\":false}";
String patternString = "[\"](?<key>.*)[\"][:][? ]?.*[{][{](?<value>.*)[}][}]";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(jsonString);
HashMap<String, Object> allMatches = new HashMap<>();
while (matcher.find()) {
allMatches.put(matcher.group("value"), matcher.group("key"));
}
System.out.println(allMatches);
I am expecting output as:
string_64|dsm_title -> title
string_16|dsm_code -> code
But the output it is giving me is:
string_16|dsm_code -> title":"{{string_64|dsm_title}}","code
Please help me in corretcting the regex. Please dont provide solution regarding parsing it to json and all. That is not the solution I need.