How to Use AI to Write and Understand Regular Expressions (Without Losing Your Mind)

Regular expressions don't have to be cryptic — learn how to use AI to write, explain, and debug regex patterns in plain English.

Regex Has Always Made Me Feel Dumb

I'll be honest with you. For the first two years of my coding journey, any time I saw a regular expression in someone else's code, I just... skipped past it. Mentally filed it under "wizardry I'll deal with later." Something like ^[\w.-]+@[\w.-]+\.\w{2,}$ might as well have been ancient Sumerian.

The problem is, regex shows up everywhere. Form validation. Log parsing. Find-and-replace scripts. Data cleaning. You can avoid it for a while, but eventually it catches up with you. And when it did catch up with me, I finally had a secret weapon: AI.

Using Claude and ChatGPT to work with regular expressions has genuinely changed how I approach them. Not just because the AI writes them for me (though it does), but because it explains them in a way that actually makes sense. This guide is going to walk you through exactly how to do that.

Step 1: Describe What You Want in Plain English

The biggest unlock with AI and regex is this: you don't have to know any regex syntax to get started. You just have to describe your problem like a human being.

Here's the kind of prompt that works really well:

prompt
# What I told Claude
I need a regex pattern that matches US phone numbers.
They can be formatted like:
555-867-5309
(555) 867-5309
5558675309
I'm using Python. Please write the pattern and explain each part.

That last part — "explain each part" — is the magic. When I first tried this, Claude came back with the pattern AND a breakdown that looked something like this:

python
# Pattern Claude gave me
pattern = r'\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}'

# Breakdown it explained:
# \(? — optional opening parenthesis
# \d{3} — exactly 3 digits (area code)
# \)? — optional closing parenthesis
# [\s.-]? — optional space, dot, or dash
# \d{3} — 3 more digits
# [\s.-]? — another optional separator
# \d{4} — final 4 digits

Suddenly that wall of symbols became readable. Each piece had a job. And because I asked for the explanation, I actually started to retain things instead of just copy-pasting blindly.

Step 2: Test It — Then Fix It With AI

Here's something nobody tells you: even AI-generated regex is often a first draft. Regex is finicky. Edge cases are everywhere. The pattern above works great — until someone types in +1 555-867-5309 with a country code prefix, and suddenly your validator breaks.

The move here is to test your pattern and bring the failures back to the AI. I do this constantly. Something like:

prompt
The pattern works for most cases, but it's not matching
numbers with a +1 country code at the start, like:
+1 555-867-5309
+15558675309
Can you update the pattern to handle those too?

Claude will update the pattern and explain what changed. You're now debugging regex in plain English, which feels like cheating in the best possible way.

Always Give AI Real Examples

The more specific test cases you include in your prompt — both strings that SHOULD match and strings that SHOULDN'T — the better the pattern you'll get back. Vague prompts produce vague regex.

Step 3: Paste Existing Regex and Ask AI to Decode It

This might be my favorite use case. You inherit someone's codebase. There's a regex in it. It looks like a cat walked across the keyboard. What now?

Paste it into your AI of choice with a simple prompt:

prompt
Can you explain what this regex does, step by step?
Also tell me what strings it would match and what it wouldn't.

^(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%]).{8,}$

Claude's response to this kind of prompt is genuinely impressive. It'll walk you through the lookaheads, the character classes, the quantifiers — in plain language. You'll find out that pattern is actually a password validator that requires at least one uppercase letter, one number, one special character, and a minimum length of 8. Useful! And now you know that, instead of guessing.

I've used this to decode regex in old PHP scripts, JavaScript form validators, and Python data pipelines. It's like having a translator on call 24/7.

Step 4: Use AI to Build a Mini Regex Test Script

Once you have a pattern you like, you'll want to test it properly before shipping it. Ask the AI to build you a quick test harness — it takes about 30 seconds and saves a lot of grief.

prompt
Write a short Python script that tests my phone number regex
against a list of valid and invalid inputs, and prints
PASS or FAIL for each one.

You'll get back something like this:

python
import re

pattern = re.compile(r'\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}')

test_cases = [
("555-867-5309", True),
("(555) 867-5309", True),
("5558675309", True),
("not-a-number", False),
("12345", False),
]

for number, expected in test_cases:
matched = bool(pattern.fullmatch(number))
status = "PASS" if matched == expected else "FAIL"
print(f"{status}: '{number}'")

Run it. See what breaks. Feed the failures back to the AI. Iterate. This loop — write, test, refine with AI — is genuinely how I get solid patterns now.

Step 5: Learn the Concepts, Not Just the Patterns

Here's where intermediate-level thinking comes in. It's tempting to just keep asking AI to generate patterns without ever building any mental model. But if you invest a little time into learning the building blocks, you'll write better prompts and spot errors faster.

A great prompt for this is:

prompt
Teach me the 10 most commonly used regex concepts
with a one-line explanation and one tiny example each.
Keep it short — I just want the essentials.

You'll get a compact cheat sheet covering things like anchors (^ and $), character classes, quantifiers, groups, and lookaheads. Bookmark it. Come back to it. Over time, these things start to stick — not because you memorized them, but because you kept seeing them in context.

Mention Your Language

Regex syntax varies slightly between Python, JavaScript, PHP, and other languages. Always tell the AI which language you're working in — it'll adjust the pattern and any flags (like re.IGNORECASE vs /i) accordingly.

The Workflow I Actually Use Now

To pull this all together, here's my current process whenever I need to write or deal with regex:

1. Describe the problem in plain English — what strings should match, what shouldn't. Include real examples.

2. Ask for the pattern plus a line-by-line explanation. Always. Even when I think I'll understand it.

3. Test it — either in a quick script the AI helps me write, or in a tool like regex101.com (which is free and great).

4. Feed failures back to the AI and refine. Usually takes one or two rounds.

5. Save the pattern and explanation together in my notes. Future-me will thank present-me.

Regex used to be the thing I dreaded most in code. Now it's just a conversation. I describe what I need, the AI drafts it, I test and iterate. The symbols still look weird sometimes — but now I know how to read them, and more importantly, I know I don't have to decode them alone.

Give it a try on the next regex you encounter. Paste it into Claude and just ask "what does this do?" I think you'll be surprised how quickly something that felt impossible starts to feel manageable.

Keep going

More tutorials in this category, or explore the full field guide.

More AI Coding Tutorials Official Docs ↗