Loading

Error 'Regex too complicated' in Apex

게시 일자: Jun 14, 2026
상세 설명

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.

솔루션

Cause 1: Matcher String Exceeds 1 Million Characters

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.

Cause 2: Pattern Is Too Complex

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.

Cause 3: String.split() on Large Strings

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().

How to Determine Which Cause Applies

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.

General Recommendations

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.

Knowledge 기사 번호

000385345

 
로드 중
Salesforce Help | Article