• A JSON string with Apex reserved words (like "currency":"USD" and otherwise undeserializable-into-Date type (like "someDate":"0001-01-01T00:00:00Z" )
  • An apex method that uses

    String s = s.replaceAll('"currency":','"currencyX":')

before deserializing into a custom Apex type with property String currencyX and Date someDate


The incoming Apex string is very large (> 1MB)


Uncatchable error: Regex Too Complicated

What to do?

1 Answer 1


This is basically teaching an old dog (me) new tricks

A cursory reading of the Apex String class documentation shows two methods

  • replace(target,replacement)
  • replaceAll(regExp,replacement)

The naive developer (me) assumes that the first method, replace, only replaces a SINGLE instance of the target string and since the use case assumes you need to replace ALL occurrences of some pattern, you should use the more powerful replaceAll(..)

But epistemic arrogance crept in and I did not realize replace(target,replacement) replaces ALL occurrences of target with replacement

Replaces each substring of a string that matches the literal target sequence target with the specified literal replacement sequence replacement.

So, if you have large strings and you are just doing simple text substitution, avoid replaceAll

String s = s.replace('"currency":','"currencyX":')
  • 1
    thx for updating your reply for those of us old farts thinking the exact same thing..
    – CRT
    Feb 2, 2021 at 18:12
  • 1
    That's not on you, that's on salesforce. They could have easily had the same method name for both methods where the regex version is just an override. Feb 26, 2023 at 1:04
  • @TemporaryFix - I think the apex methods are just borrowed/mimicked from Java
    – cropredy
    Feb 26, 2023 at 5:04
  • This answer is probably better: salesforce.stackexchange.com/questions/321918/…
    – Shanerk
    Feb 9 at 15:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .