I have a json string like this:
string = "{name={first=sam, last=vo}, hobbies={hobby1=football, hobby2=swimming}}"
And I want to remove the "name=" and the "hobbies=", so that I use this pattern: \w*\=(?={)
->tested using editPadPro
However, when I use the replace all in java:
String pattern = "\\w*\\=(?={)";
String removedParent = string.replaceAll(pattern, "");
I got this error message
"Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 7
\w*\=(?={)"
Can you please give me some advices to get this work?
Regards,
Sam
The problem is that the {
character is a special character in regex syntax which denotes an amount (for instance \d{2}
denotes 2 digits). In your case, you want to match the literal {
, meaning that you need to escape the {
character, so you need to change your regex to this: "\\w*\\=(?=\\{)";
.
For me, this yielded:
{{first=sam, last=vo}, {hobby1=football, hobby2=swimming}}