[SalesForce] “REGEX too Complicated” error

Is "REGEX too Complicated" error while attempting to upload a CSV via Apex into SFDC an undocumented limit? What should be the optimal data volume for a CSV upload via Apex? I was trying with 10,000 rows of 8 columns each?

Best Answer

Looks like you found the solution, but for other folks, I've seen this error occur in three different ways.

  1. Calling String.replace() on very large strings (somewhere around 500k from memory, but it varied based on heap usage). This is extra confusing since String.replace() doesn't support regexes. The solution is to chunk up the string into smaller chunks and call replace on those.
  2. Calling String.replaceAll() or the Pattern/Matcher classes on very large strings. Similar solution to #1; in both cases, be careful that your chunk-and-replace algorithm doesn't skip replacements of target strings that span two chunks.
  3. Calling String.replaceAll() or the Pattern/Matcher classes using a regex that has too much branching logic. Simplest repro I've seen is to take a large multi-line file and run a match on the regex (\n|\r|.) (seems silly but this is a valid regex since Apex doesn't support DOTALL). In all these cases I've seen, I've had to refactor the regex to actually make it less complex, although "complex" is a misnomer in the example here.
Related Topic