How to improve this regex to avoid PREG_BACKTRACK_LIMIT_ERROR?

Sanky

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.

Casimir et Hippolyte

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]

編集
0

コメントを追加

0

関連記事

分類Dev

How to improve the regex?

分類Dev

How to improve this code? Extract info using Regex

分類Dev

Javascript Regex: How to avoid catastrophic backtracking in this case

分類Dev

How to improve speed for time limit exceeded

分類Dev

How to avoid printing an error in BASH?

分類Dev

How do i improve my regex to grep third level domain but not extra character at last?

分類Dev

How to fix javavascript error from browser and improve error logging

分類Dev

On Error GoTo : How to avoid goto if no error

分類Dev

How to improve this "update" function?

分類Dev

How to help improve Banshee?

分類Dev

PREG_BACKTRACK_LIMIT_ERRORを回避するためにこの正規表現を改善するにはどうすればよいですか?

分類Dev

Bitwise operation performance, how to improve

分類Dev

How to improve PostgreSQL performance on INSERT?

分類Dev

How to improve notes database performance?

分類Dev

How to improve the performance of the regular expression?

分類Dev

How to improve disk space usage?

分類Dev

Regex in search & replace: avoid fixed length of lookaround

分類Dev

How to avoid duplicate key error in swift when iterating over a dictionary

分類Dev

How to avoid the Jackrabbit DataStore "table or view does not exist" error?

分類Dev

How to avoid room foreign key error - constraint failed (code 787)

分類Dev

How do I avoid the 'NavigableString' error with BeautifulSoup and get to the text of href?

分類Dev

how to avoid the error 42702 (AMBIGUOUS COLUMN ) with knex and postgres

分類Dev

How to avoid/check this very sinister error-source in C++

分類Dev

How to read files and avoid coercing to unicode error in python?

分類Dev

How to avoid duplicate node?

分類Dev

Unreachable if statement, how to avoid

分類Dev

how to avoid automatic submission

分類Dev

How to avoid over fitting?

分類Dev

How avoid duplication in a list

Related 関連記事

  1. 1

    How to improve the regex?

  2. 2

    How to improve this code? Extract info using Regex

  3. 3

    Javascript Regex: How to avoid catastrophic backtracking in this case

  4. 4

    How to improve speed for time limit exceeded

  5. 5

    How to avoid printing an error in BASH?

  6. 6

    How do i improve my regex to grep third level domain but not extra character at last?

  7. 7

    How to fix javavascript error from browser and improve error logging

  8. 8

    On Error GoTo : How to avoid goto if no error

  9. 9

    How to improve this "update" function?

  10. 10

    How to help improve Banshee?

  11. 11

    PREG_BACKTRACK_LIMIT_ERRORを回避するためにこの正規表現を改善するにはどうすればよいですか?

  12. 12

    Bitwise operation performance, how to improve

  13. 13

    How to improve PostgreSQL performance on INSERT?

  14. 14

    How to improve notes database performance?

  15. 15

    How to improve the performance of the regular expression?

  16. 16

    How to improve disk space usage?

  17. 17

    Regex in search & replace: avoid fixed length of lookaround

  18. 18

    How to avoid duplicate key error in swift when iterating over a dictionary

  19. 19

    How to avoid the Jackrabbit DataStore "table or view does not exist" error?

  20. 20

    How to avoid room foreign key error - constraint failed (code 787)

  21. 21

    How do I avoid the 'NavigableString' error with BeautifulSoup and get to the text of href?

  22. 22

    how to avoid the error 42702 (AMBIGUOUS COLUMN ) with knex and postgres

  23. 23

    How to avoid/check this very sinister error-source in C++

  24. 24

    How to read files and avoid coercing to unicode error in python?

  25. 25

    How to avoid duplicate node?

  26. 26

    Unreachable if statement, how to avoid

  27. 27

    how to avoid automatic submission

  28. 28

    How to avoid over fitting?

  29. 29

    How avoid duplication in a list

ホットタグ

アーカイブ