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?
Looks like you found the solution, but for other folks, I've seen this error occur in three different ways.
- 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.
- 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.
- 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.
I got the below posts which exactly addresses the problem ... it might be useful to others facing the issue....