I think you are mixing backlinks like \1 and capture groups (...) .
Backreferences prevents all kinds of optimizations, making the language irregular.
Capturing groups makes the regex engine work a little more to remember where the group starts and ends, but not as bad as backlinks.
http://www.regular-expressions.info/brackets.html explains group collection and backlinks to them in detail.
EDIT:
In backlinks that make regular expressions irregular, consider the following regular expression that matches the lua comments:
/^--(?:\[(=*)\[[\s\S]*?(?:\]\1\]|$)|[^\r\n]*)/
So --[[...]] - comment, --[=[...]=] - comment, --[==[...]==] - comment. You can insert comments by adding extra equal characters between square brackets.
This cannot be matched in strictly ordinary language , therefore a simple state machine cannot process it in O (n) time - you need a counter.
Perl 5 regular expressions can handle this with backlinks. But as soon as you need uneven pattern matching, your regular expression library should abandon the simple state-based approach and use more complex and less efficient code.
source share