I have following regex.
(From:.*)<(.+) (.+)@first abc.com>
And I am using following code to parse email address
$mimeFile = preg_replace_callback( "/(From:.*)<(.+) (.+)@first abc.com>/",
function($matches) {
return $matches[1].'<'.strtolower($matches[2]).'.'.strtolower($matches[3]).'@xyz.com>';
}, $content );
It works for small content, but when content is large, it gives a PREG_BACKTRACK_LIMIT_ERROR.
How can I improve my regex?
Thanks in advance.
You can use this:
$mimeFile = preg_replace_callback('~From:[^<\n]*<\K([^>@ ]+) ([^>@]+)@first abc\.com>~', function($m) {
return strtolower($m[1] . '.' . $m[2]) . '@xyz.com>';
}, $content );
The problem of your pattern comes from the use of consecutive .*
or .+
that are too permissive and that cause a catastrophic backtracking when the space or the substring @first abc.com>
are not present.
In the two examples, instead of using .+
, I use character classes that exclude the character where I need to stop. [^@>]+
will stop as soon as @
or >
is encountered.
Note: When you write something like [^a]+a
, the pcre regex engine automatically optimizes it as [^a]++a
(it makes the quantifier possessive)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加