Olcan Teke
Olcan Teke

Reputation: 58

Catastrophic backtracking

i have some problems with my regex:

if (preg_match_all('/{[a-z]+:ce_img:(single|pair)(\s.*)*}/', $files, $matches))
{
   echo "ok";
}

For some reason it crashes my site. Ofcourse i already tried to google it and found something about "Catastrophic backtracking" although i'm not sure if this is my problem.

The regex should give me everything between {eggs:ce_img:single(or pair) till the ending }

When i try to change or remove (single|pair) it runs just normally. So it should be something regarding that right?

I'm quite sure that $files isn't the problem.

Does someone know how to solve this?

Regards, Olcan

EDIT: Here an example of how this regex should work: image

Upvotes: 0

Views: 734

Answers (2)

revo
revo

Reputation: 48751

Your RegEx crashes your site (due to catastrophic backtracking) because your input file contains at least one of these:

  1. Multiple space characters after target block
  2. A sequence of characters with spaces between after target block. Similar as \s.+.

Solution:

{[a-z]+:ce_img:(?:single|pair)(?:\s+[\w-]+="[^"]*")*\s*}

This specifically matches your pattern. Explanation of last different part:

(?:                    # Start of non-capturing group (a)
    \s+[\w-]+="[^"]*"  # Match similar following string `attr="value"`
)*                     # Many or zero times - end of non-capturing block (a)
\s*                    # Match all space characters if any, before closing brace `}`

Upvotes: 2

user1134181
user1134181

Reputation:

This should work:

{[a-z]+:ce_img:(?:single|pair)?([\w\W\s]+)*}

For the text that presented in your image:

{eggs:ce_img:single 
   src="{src}"
   fallback_src="/assets/a-b-c.jpg"
   width="250"
   height="250"
   add_dims="no"
   crop="yes"
   title="{title}"
   alt="{title}"
   allow_scale_larger="yes"
}

You'll get:

Group 1     src="{src}"
            fallback_src="/assets/a-b-c.jpg"
            width="250"
            height="250"
            add_dims="no"
            crop="yes"
            title="{title}"
            alt="{title}"
            allow_scale_larger="yes"

See demo here: https://regex101.com/r/uo9Kqi/1

Upvotes: 1

Related Questions