A Scenario Every Developer Knows
Have you ever had this experience: the product manager sends you a message saying, "Please validate phone numbers, emails, and ID card numbers in user input." Sounds simple—you open your editor and start writing regex.
Phone numbers are easy: 1[3-9]\d{9}. Emails start getting complicated. ID card numbers require handling both the 15-digit old format and the 18-digit format with a check digit, plus guarding against special characters users might deliberately enter. After finishing, you're not sure it's actually correct, so you search for "online regex tester" and paste in a dozen test cases one by one, only to find that the email pattern lets test@localhost through, or the ID card check digit was never computed at all.
An hour has passed, the regex keeps growing longer, and you start questioning your life choices. This isn't a skill problem—regex is inherently a "write it and forget it" thing; no one can remember all the edge cases long-term. The pain points boil down to three things: you can't write it, you can't read it, and you can't test it thoroughly.
What AI Can Actually Do for You
I eventually handed this task entirely over to AI, with a fixed four-step process that takes about three minutes each time:
Step 1: State your requirements clearly and let AI generate the regex. No need to wrestle with syntax yourself—describe the matching rules, the data source, and the pitfalls to avoid in plain language. The AI will produce a regex and—this is crucial—include explanations for each segment.
Step 2: Have AI list the edge cases itself. This is the step I find most valuable. Humans miss edge cases, but if you explicitly ask AI to list "10 examples that should match and 10 that shouldn't," the list it produces often includes cases you never thought of: full-width digits, phone numbers with spaces, ID numbers ending in x, overly long emails, emoji mixed in.
Step 3: Run a verification script. Feed the regex and test cases to AI or a local script, run all assertions at once, and only pass when everything goes green.
Step 4: Ask for defensive recommendations. For example, which scenarios should use capture groups for extraction instead of whole-match validation, and whether the regex might suffer catastrophic backtracking on extreme inputs.
A Reusable Prompt Template
你是一位资深后端工程师,精通正则表达式的编写与测试。
我的需求:
【匹配目标】:(描述要匹配的内容,如:中国大陆手机号)
【数据来源】:(如:用户注册表单的自由输入)
【必须匹配】:(列出应命中的例子)
【必须排除】:(列出不应命中的例子,尽量刁钻)
【运行环境】:(如:Python re / JavaScript / PCRE,说明差异)
请你完成以下工作:
1. 给出正则表达式,并逐段解释含义;
2. 额外补充至少10个边界测试用例(5个应命中、5个应排除),
包括我没想到的情况,如全角字符、空格、超长输入;
3. 用伪代码展示验证流程,确保每个用例都有断言;
4. 指出该正则的已知局限,以及是否存在回溯风险;
5. 如果我的需求本身有歧义,先提问再给答案。Point 5 is especially worth keeping—it makes AI ask clarifying questions when your requirements are unclear, instead of confidently handing you a wrong regex.
Before and After: The Numbers Speak
According to my own records, for the same "phone number + email + ID card" validation triple:
| Step | Writing It Myself | Using AI |
|---|---|---|
| Writing the initial regex | 25 minutes | 1 minute |
| Thinking up edge cases | Usually can't think of any | 20 generated in 2 minutes |
| Testing one by one with online tools | 40+ minutes | 1-minute script run |
| Post-launch bad case rework | Average of 1, ~30 minutes | Essentially zero |
| Total | ~1.5 hours | ~4 minutes |
Saving 80+ minutes at a time may not sound like much, but regex needs come up frequently: form validation, log extraction, data cleaning, crawler parsing, comment filtering... It's normal for an indie developer to encounter them five or six times a month. At 6 times per month, that's about 8 hours saved monthly—an entire workday. For a small team where three people all write regex, the time cost adds up considerably over a year.
What's harder to quantify but equally important is certainty. I used to feel uneasy deploying regex to production; now I deploy with assertions covering 20 edge cases every time, and rework has nearly disappeared. Saving time is one thing, but the real win is avoiding repeated debugging and "getting woken up by alerts at 3 AM."
Three Practical Tips
- Always specify the runtime environment. The same regex can behave differently in JavaScript versus Python, especially with Unicode handling. Don't delete that line in the prompt template.
- Let AI generate edge cases, but you do the final review. AI-generated test cases occasionally go collectively off track (e.g., all following US phone number formats)—scanning the list takes only 10 seconds.
- Don't force regex where it doesn't belong. For logic like ID card check digits, AI will also recommend computing it in code instead of cramming it into regex—listen to it.
Final Thoughts
Regex was one of the earliest and most directly rewarding scenarios where I incorporated "AI-assisted development" into my daily workflow: the requirements are clear, verification is objective, right and wrong are obvious, and AI can hardly bluff its way through. If you're new to AI coding assistance, starting with this scenario has virtually zero learning cost.
If you don't have a go-to AI tool account yet, go register for one and try pasting the template above directly:
https://api.thistoken.ai/register
The first time you see all 20 test cases turn green, you'll understand what this article is talking about.
---
Ready to try it yourself? Sign up at https://api.thistoken.ai/register to get your API key and start building.
Vous voulez essayer Token.AI ?
Créez une API Key au niveau du projet, activez les canaux dans la console et configurez le routage, les budgets et les journaux d'audit.
注册 ThisToken.AI 并获取 API Key