Here are some tips to keep in mind while handling situations with catastrophic or excessive backtracking in your regex:
- When you write regular expressions, make sure they fail fast without spending a lot of unnecessary steps in backtracking.
- When using nested repetition operators or quantifiers, make sure that there is only one unique way to match the a string.
- Make good judicious use of atomic groups and possessive quantifiers to avoid excessive backtracking.
- You should avoid having too many optional matches that are not mutually exclusive in an alternation pattern.
- Be very careful when using a free-flowing pattern such as .* or .+ in your regex. Wherever possible, use negated character classes for ...