Salesforce の Apex コードにおいて、正規表現処理がシステムのリソース制限を超過すると、Apex エラー「System.Exception: Regex too complicated」が発生します。主な原因は以下の 3 つです。
このエラーは try/catch ブロックで捕捉することができず、即座に処理が終了します。この記事では、それぞれの原因について説明し、具体的な解決アプローチを提供します。
Salesforce では、正規表現の入力シーケンスにアクセスできる回数が 1,000,000 回に制限されています。Matcher 文字列がこの制限を超えると、実行時エラーが発生します。この原因を解決するには、正規表現処理の前に長さのチェックを追加し、文字列が 1,000,000 文字未満である場合のみ処理を実行します。文字列が長すぎる場合は、処理の前に小さなセグメントに分割します。
(A)?(B)?(C)?... のようなパターンが 50 回以上繰り返されるなど、多数のオプショングループを持つパターンは、どのような入力文字列でも失敗します。このタイプのパターンは指数のバックトラッキングを引き起こします。この原因を解決するには、オプショングループを減らす、選択ロジックを再構築する、またはパターンを単一の結合された式として適用するのではなく、連続して適用される小さなサブパターンに分解することによってパターンを単純化します。
非常に大きな文字列に対して String.split() を呼び出すと、正規表現のアクセス制限を超過する可能性があります。この原因を解決するには、Apex の DataWeave を使用して非常に長い文字列を解析するか (API バージョン 59.0 以降で利用可能)、split() を呼び出す前に入力文字列を小さなセグメントに分割します。
データに関係なく同じ入力でエラーが毎回発生する場合は、パターンが複雑すぎます (原因 2)。パターンを単純化してください。
エラーが時々発生する場合や、特定の大きなデータで発生する場合は、Matcher 文字列が長すぎるか (原因 1)、長い文字列に対して String.split() が呼び出されています (原因 3)。長さのチェックを追加してください。
100 万文字のアクセス制限に達する可能性を最小限に抑えるには、次の手順を実行します。
(1) 処理される入力シーケンスを削減する。(2) パターン (特に冗長な選択肢) を削減、結合、または削除する。(3) バッチ全体を処理する必要がある場合は、入力シーケンスを半分に分割し、後半には @future 呼び出しを使用してそれぞれの半分を個別に処理することを検討する。このアプローチは非同期制限の対象であり、特定のユースケースに依存することに注意してください。
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.