0

Sometimes I will be faced with catastrophic backtracking when I'm writing regex pattern. For example, in this content, as you see pattern below matches one thing as well:

/Jakob Finch/

But pattern below throws catastrophic backtracking error:

/style.*?Jakob Finch/

Why? And how can I fix it?

stack
  • 10,280
  • 19
  • 65
  • 117
  • 1
    Use proper tools to parse HTML/XML. Even the best unrolled regex (like [this one](https://regex101.com/r/HVnqSH/1)) can cause slowdown/catastrophical backtracking depending on the input. – Wiktor Stribiżew Jan 10 '17 at 08:40
  • @WiktorStribiżew I don't want to use regex to parse XML. I'm just trying to understand when exactly **catastrophic backtracking** error occurs? And why? And how can I fix it? – stack Jan 10 '17 at 08:43
  • There is an SO question that deals with catastrophic backtracking in general. Let me find it... – Wiktor Stribiżew Jan 10 '17 at 08:47
  • @WiktorStribiżew Thank you .. if you find it let me know .. however I hope it wasn't [this](http://stackoverflow.com/questions/29751230/regex-pattern-catastrophic-backtracking) – stack Jan 10 '17 at 08:48
  • Well, please check [Catastrophic backtracking shouldn't be happening on this regex](http://stackoverflow.com/questions/7378625/catastrophic-backtracking-shouldnt-be-happening-on-this-regex). I think [Alan Moore's answer](http://stackoverflow.com/a/7379269/3832970) can be applied here. – Wiktor Stribiżew Jan 10 '17 at 08:59

0 Answers0