Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java replaceAll Illegal repetition

I have a big json stored in a string. I want to change some part of the String and I get this error:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 6 "date":{"nil":"true"} ^ at java.util.regex.Pattern.error(Pattern.java:1924) at java.util.regex.Pattern.closure(Pattern.java:3104) at java.util.regex.Pattern.sequence(Pattern.java:2101) at java.util.regex.Pattern.expr(Pattern.java:1964) at java.util.regex.Pattern.compile(Pattern.java:1665) at java.util.regex.Pattern.(Pattern.java:1337) at java.util.regex.Pattern.compile(Pattern.java:1022) at java.lang.String.replaceAll(String.java:2162) at basari.process.MsisdnProcess.setAllPropTypes(MsisdnProcess.java:51) at testClass.MainTest.main(MainTest.java:98)

My code:

String example =  "deviceInfo":{"deviceBrand":"NOKIA","imei":"11111111","deviceModel":"6300","date":{"nil":"true"}}

example.replaceAll( "\"date\":{\"nil\":\"true\"}", "\"date\":\"2014-08-14T10:00:00.000+02:00\"");
like image 715
hurricane Avatar asked Jan 31 '26 08:01

hurricane


1 Answers

You're probably looking for String#replace, instead of replaceAll (which uses regex).

You get this exception because in regex, {...} is a quantifier. For example:

  • {1,3} = 1 to 3 times
  • {3,} = 3 or more times
  • {3} = exactly 3 times
like image 60
August Avatar answered Feb 01 '26 21:02

August



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!