0

I need a regex that does the following replacements:

"\"Id\":\"123Abc\",\"foo\":\"bar\"" -> "\"Id\":null,\"foo\":\"bar\"" (change value for only field \"Id\" into null)
"\"Id\":123" -> "\"Id\":null" (works for numbers too)
"\"Id\":null" -> "\"Id\":null" (if already null, do nothing)
"\"foo\":\"bar\"" - > "\"foo\":\"bar\"" (if \"Id\" not present, do nothing)

I came up with \\\"Id\\\":([^]+), and on https://www.regextester.com it matches my string, but I tried turning it into Java code and nothing happens to the string.

str.replaceAll("\\\"Id\\\":([\\^]+)", "\\\"Id\\\":null");
9
  • Your character class for the id to be nullified may start with the caret sign followed by double quote. Commented Aug 5, 2019 at 21:46
  • 5
    Why aren't you using a JSON parser? Commented Aug 5, 2019 at 21:49
  • If \" is a delimiter, how do you check for an escaped delimiter ? Commented Aug 5, 2019 at 22:17
  • 2
    @onepiece But if the value of Id can be a string literal, that literal can contain escaped characters, so writing a regex is complex. What you should consider, more than performance, is correctness. A JSON parser will parse correctly. A regex may go wrong if there's some condition you forgot to check. Use a parser!! Don't waste more time on this, unless you learn thru profiling that performance is too slow. Beware premature optimizations, especially where optimizations may cause incorrect behavior. Commented Aug 5, 2019 at 22:50
  • 2
    To quote Jamie Zawinski: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. Commented Aug 5, 2019 at 22:55

1 Answer 1

1

The caret sign ( ^ ) has to be the first symbol inside the square brackets to have its inverting effect, so that [^\]+ matches any character as long as it's not the backslash.

Also, the id string to be nullified is going to be present with double quotes so we have to match those as well and escape them : \"[^\]+\"

Moreover, Java needs its additional escaping so wee endup with \\\"[^\\]+\\\"

Finally , I would go with something like this :

str.replaceAll("\\\"Id\\\":\\\"([^\\]+)\\\"", "\\\"Id\\\":null");

Note that you may have to add optional space character depending on the input which are JSON compliant.

str.replaceAll("\\\"Id\\\"\s*:\s*\\\"([^\\]+)\\\"", "\\\"Id\\\"\s*:\s*null");

For references, you can read through https://www.regular-expressions.info/refquick.html

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.