On the Complexity of the Matching Problem of Regular Expressions with Backreferences
This paper establishes the fine-grained computational complexity of matching regular expressions with backreferences by proving conditional lower bounds under SETH and triangle detection assumptions while presenting an improved algorithm for 1-use backreferences.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
The Big Picture: The "Regex" Traffic Jam
Imagine you are a security guard at a club (the computer system). You have a list of rules (a Regular Expression) for who can get in.
- Simple Rules: "Only people wearing red shirts." This is easy to check. You look at a shirt, say "Red? Yes, come in." It takes the same amount of time whether the line is 10 people or 10,000.
- The Problem (ReDoS): Sometimes, hackers craft a specific line of people that tricks the guard into doing a massive amount of unnecessary work. Instead of checking one person and moving on, the guard starts checking Person A, then Person B, then Person A again, then Person C, then Person A again... until the guard collapses from exhaustion. This is called a Denial of Service (ReDoS) attack.
In the real world, this has caused massive websites like Stack Overflow and Cloudflare to crash. The paper notes that even "quadratic" slowness (where checking 100 people takes 10,000 steps) is enough to crash a system.
The Villain: "Backreferences"
Standard rules are simple. But modern "Regex" engines have a super-powerful feature called Backreferences.
The Analogy:
Imagine a rule that says: "Find a word, remember it, and then make sure the exact same word appears again later."
- Example: "Find a word, call it 'X'. Then, find 'X' again."
- If the input is
apple ... apple, it works. - If the input is
apple ... banana, it fails.
This feature is incredibly useful for programmers, but it makes the "guard's" job much harder. The guard has to remember what they saw earlier and constantly compare it to what they are seeing now. The paper asks: Can we build a guard who is fast enough to handle these complex rules without getting tired?
The Paper's Findings: The Good, The Bad, and The Ugly
The authors investigated exactly how hard it is to solve these matching problems. They broke it down into two sides: Hardness (Why it's difficult) and Algorithms (How to fix it).
1. The Bad News: Some Rules are Impossible to Speed Up
The paper proves that for certain types of complex rules, there is no "magic bullet" to make them fast.
- The "Triangle" Problem: They showed that if you have a rule that uses two variables (like remembering two different words and checking them later), solving it is as hard as finding a triangle in a giant social network graph. If you could solve the rule quickly, you could solve the graph problem quickly. Since graph experts believe the graph problem is inherently slow, the rule problem must be slow too.
- The "Orthogonal Vectors" Problem: For rules with even more variables, they proved that the time required grows exponentially with the number of variables. It's like trying to find a specific combination of keys in a lock; the more keys you have, the impossible it becomes to brute-force it quickly.
Takeaway: If your rule is too complex (using many "remember this" features), you cannot build a fast engine for it. You will always hit a wall.
2. The Good News: A "Near-Linear" Solution for Simple Cases
However, the paper found a sweet spot. They focused on a specific, common type of rule:
- The "ABCBD" Pattern: "Find a word (A), then a word (B), then a word (C), then the exact same word B again, then a word (D)."
- Real-world example: "Find a username, then a password, then a message, then the same username again, then a signature."
The authors discovered that while this looks tricky, it can be solved very efficiently.
- The Old Way: Previous methods were like checking every possible combination in a library, which took time (quadratic). If the book was 1,000 pages, it took 1,000,000 steps.
- The New Way: The authors built a new algorithm that takes roughly time.
- The Analogy: Imagine the library is organized with a magical index system (using Suffix Trees and Factorization Forests). Instead of reading every page, the guard can jump straight to the relevant sections. If the book is 1,000 pages, the new method takes roughly 10,000 steps (or even fewer), which is a massive improvement.
How the New Algorithm Works (The "Magic Tricks")
To achieve this speed, the authors used several clever techniques, which they describe in the paper:
- The Suffix Tree (The Map): They built a giant map of the input string. This map shows every possible ending of the string. It helps the guard instantly see, "Oh, this word 'B' appears here, and it also appears there."
- Heavy-Light Decomposition (The Sorting Hat): They split the map into "heavy" paths (very common paths) and "light" paths (rare paths). They only do the heavy lifting on the rare paths, saving time.
- Periodicity (The Rhythm): They noticed that when a word repeats (like "B...B"), the string often has a rhythm or a pattern. They used math to predict these patterns instead of checking every single letter.
- Factorization Forests (The Index): This is a data structure that acts like a super-fast index, allowing the guard to check if a chunk of text matches a rule in constant time, no matter how long the text is.
Summary of the Conclusion
- Can we stop all ReDoS attacks? No. If a rule is too complex (too many "remember this" variables), it is mathematically proven to be slow.
- Can we fix the most common complex rules? Yes! For the specific case where a rule remembers one word and checks for it once later (the "ABCBD" pattern), the authors created a new engine that is almost as fast as the simple rules.
- Why does this matter? It tells software engineers: "Don't use too many backreferences, or you'll be slow. But if you use them in this specific, common way, you can now use our new method to keep your system safe and fast."
The paper essentially draws a line in the sand: Here is where the speed limit is unbreakable, and here is where we found a way to drive faster.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.