Regular expressions (regexes) are the Swiss Army knife of text processing, yet they remain notoriously difficult to write and even harder to read. We’ve all been there: staring at a string of backslashes and quantifiers, wondering why it’s not matching that one edge case. This paper introduces TransRegex, a framework that aims to end the regex struggle by combining the intuition of natural language with the precision of test examples.
TL;DR
TransRegex is a "generate-and-repair" framework that synthesizes regular expressions by first translating natural language descriptions into a candidate regex and then automatically repairing that candidate using user-provided examples. By treating synthesis as a multi-modal repair problem, it achieves up to 39% higher accuracy than state-of-the-art natural language models and significantly outperforms existing multi-modal tools on complex, real-world datasets.
What Problem Does This Paper Address?
Automating regex synthesis usually falls into two camps:
- Natural Language (NL) Synthesis: You describe what you want in English, and a model generates the code. The problem? NL is ambiguous. A "vowel preceding a numeral" could mean many things, and models often hallucinate invalid syntax.
- Programming by Example (PBE): You provide strings that should and shouldn't match. The problem? It’s hard to provide enough examples to cover every edge case, often leading to "overfitting" where the regex works for your examples but fails on everything else.
Existing "multi-modal" approaches (using both NL and examples) often rely on intermediate "sketches"—incomplete regexes with holes. If the initial sketch is wrong, the final regex is doomed. TransRegex breaks this cycle by allowing the model to generate a full (potentially imperfect) regex and then using a dedicated repair algorithm to fix it.
Methods
TransRegex operates in two main stages, as shown in the overview below:

1. The Generator: $S_2RE$
The authors built a sequence-to-sequence model called $S_2RE$ (Sequence-to-Regular-Expression). Unlike standard models that only try to match the "ground truth" regex, $S_2RE$ uses Policy Gradient reinforcement learning to reward two things:
- Semantic Correctness: Does it match the intended logic?
- Syntactic Validity: Is it actually a valid regex that won't crash a compiler?
If the model still produces an invalid string, a secondary "invalid2valid" model attempts to patch the syntax.

2. The Repairer: $Syncorr$
If the generated regex doesn't pass the user's examples, the $Syncorr$ algorithm takes over. It uses Neighborhood Search (NS) to explore variations of the regex.
- Abstraction: To avoid breaking parts that work, $Syncorr$ abstracts common patterns (like
[0-9]or[a-z]) into symbolic tokens. - Transformation: It then applies "mutations"—like changing a quantifier from
{3,}to{5,}or swapping the order of elements—to find a version that perfectly matches all positive and negative examples.
Datasets
The authors evaluated TransRegex on three major benchmarks:
- KB13: A classic dataset of 824 expert-written NL-regex pairs.
- NL-RX-Turk: A large-scale dataset of 10,000 crowdsourced pairs.
- StructuredRegex: The most challenging set, featuring long, complex English descriptions and 12 examples (6 positive, 6 negative) per task.
Key Insights and Ideas
- Validity Matters: By explicitly rewarding syntactic validity during training, $S_2RE$ ensures that the repair step always has a "legal" starting point. On the complex StructuredRegex dataset, $S_2RE$ achieved 100% validity, while baselines struggled at around 50%.
- Repair as Disambiguation: The paper treats examples not just as constraints, but as a way to resolve NL ambiguity. If a user says "at least 3 times," the examples clarify exactly which part of the pattern that quantifier applies to.
- Preserving Integrity: $Syncorr$’s use of rewriting rules to treat sub-regexes as atomic units is a clever way to reduce the search space and prevent the "overfitting" common in example-based synthesis.
Key Results
TransRegex sets a new high bar for accuracy across the board. As shown in the table below, it consistently outperforms both pure NL models and previous multi-modal attempts.
- Accuracy Boost: On the difficult StructuredRegex dataset, TransRegex reached 67.4% accuracy, nearly doubling the performance of the previous state-of-the-art multi-modal tool (37.2%).
- Efficiency: While the repair process adds overhead, the tool remains practical, taking between 4 to 13 seconds on average to synthesize a complex regex.
Limitations and Future Work
The authors identify three main failure modes:
- Uncharacteristic Examples: If the user's examples are too generic (e.g., providing
abcfor a regex that should match any three letters), the repairer can't distinguish between the correct and incorrect logic. - Wrong Examples: If a user provides a "positive" example that shouldn't actually match, the tool will faithfully (but incorrectly) "fix" the regex to accommodate the error.
- Extreme Complexity: Very long descriptions or deeply nested regex structures can still lead to a search space explosion that exceeds the tool's time limits.
Future work may involve integrating even more powerful repair tools and improving the model's ability to handle extremely long, multi-sentence natural language specifications.
Summary updated