What are some of the reasons for getting the "Regex too complicated" error?
The error you are getting [System.Exception: Regex too complicated] happens on two different events:
1. Your Matcher is too complex.
Salesforce limits the number of times an input sequence for a regular expression can be accessed to 1,000,000 times. If you reach that limit, you receive a runtime error.
Therefore the Matcher string can’t be longer than 1M characters. In this case, you should check the length of the Matcher string to prevent it from running if the string is over 1M characters.
2. Your Pattern is too complex
This is an example of a complex Pattern:
Pattern pat = Pattern.compile('(A)?(B)?(C)?(D)?(E)?(F)?(G)?(H)?(I)?(J)?(K)?(L)?(M)?(N)?(O)?(P)?(Q)?(R)?(S)?(T)?(U)?(V)?(W)?(X)?(Y)?(Z)?(AA)?(AB)?(AC)?(AD)?(AE)?(AF)?(AG)?(AH)?(AI)?(AJ)?(AK)?(AL)?(AM)?(AN)?(AO)?(AP)?(AQ)?(AR)?(AS)?(AT)?(AU)?(AV)?(AW)?(AX)?(AY)?(AZ)?$');
Matcher mat = pat.matcher('asdfasdfasdfasdfasdfasdf');
This type of pattern will fail with any string.
3. You are attempting to use String.split() on too large of a stringLarge strings can cause this limit to be exceeded when using String.split(). In this case, the recommended approach would be to explore using DataWeave to parse the string or alternatively, only call split() on smaller-size strings.
As you can see, this error can happen because one of three options (described above). As a rule of thumb, If the error happens always, then the problem is with the pattern and the solution is to simplify the pattern. If the problem only happens sometimes, then the matcher is longer than 1M characters and the string should be split before it is processed.
The operation is so time consuming for our servers that if we detect this exception, we immediately kill the process because your process has already spent too much CPU time. This is the reason the error can’t be trapped, but good coding practices should avoid users from seeing this error.
In general, one or more of the following recommendations will need to be implemented as to minimize the likelihood of encountering the 1 million character access limit:
1. The input sequence can be reduced
2. The number of patterns being compared against the input sequence can be reduced, combined or eliminated -- particularly redundancies
3. If the batch must all be processed, you can occasionally obtain some leeway here by splitting up the input sequence into halves, cutting the input sequence in half and finding the next period after the half-way point, processing that as one batch and processing the remainder as a second batch via a @future call would be an option. (This is subject to asynchronous limits, so this approach is largely dependent on use cases, available asynchronous calls, etc.)
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.