The Apex error [System.Exception: Regex too complicated] occurs in Salesforce Apex code when a regular expression operation exceeds system resource limits. There are three primary causes: (1) the Matcher string exceeds 1 million characters; (2) the Pattern itself is too complex, such as a long chain of optional alternation groups; or (3) String.split() is called on an excessively large string. This error cannot be caught with a try/catch block — it immediately terminates the process. This article explains each cause and provides specific resolution approaches.
Salesforce limits the number of times an input sequence for a regular expression can be accessed to 1,000,000 times. If your Matcher string exceeds this limit, you receive a runtime error. To resolve this cause: add a length check before the regex operation and only proceed if the string is under 1,000,000 characters. If the string is too long, split it into smaller segments before processing.
A Pattern with many optional groups — for example, a pattern like (A)?(B)?(C)?... repeated 50 or more times — will fail with any input string. This type of pattern causes exponential backtracking. To resolve this cause: simplify the Pattern by reducing optional groups, restructuring the alternation logic, or breaking the pattern into smaller sub-patterns applied sequentially rather than as one combined expression.
Calling String.split() on a very large string can cause the regex access limit to be exceeded. To resolve this cause: use DataWeave in Apex to parse large strings (available as of API version 59.0), or split the input string into smaller segments before calling split().
If the error occurs every time with the same input regardless of data: the Pattern is too complex (Cause 2) — simplify the pattern. If the error only occurs sometimes or with certain large data: the Matcher string is too long (Cause 1) or String.split() was called on a large string (Cause 3) — add length checks.
To minimize the likelihood of encountering the 1 million character access limit: (1) Reduce the input sequence being processed. (2) Reduce, combine, or eliminate patterns — particularly redundant alternations. (3) If the batch must all be processed, consider splitting the input sequence in half and processing each half separately, using a @future call for the second half. Note that this approach is subject to asynchronous limits and depends on your specific use case.
000385345

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.