Generate Regex Patterns by Describing Them in Plain English
Regex is powerful but notoriously hard to read and write correctly under pressure. Claude can handle the syntax while you focus on what you actually need to match.
Just describe the pattern in plain English:
Write a regex that matches UK postcodes in these formats:
SW1A 1AA, W1A 0AX, EC1A 1BB — with an optional space in the middle.
It should match the full string, not just find it inside a longer string.
Give me the pattern for JavaScript and also for PHP.
Claude will produce the pattern, explain each component, and often include a few test cases showing what it matches and what it doesn't:
// JavaScript
const uk_postcode = /^[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}$/i;
You can also go the other way — paste a gnarly regex you've inherited and ask Claude to explain what it does:
What does this regex match? Explain each part:
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
This is also great for refining patterns — ask Claude to modify an existing regex to handle a new edge case without breaking what already works.
If you can describe what it should match, Claude can write the regex — and explain it so you actually understand it too.
Log in to leave a comment.
Before jumping to implementation, describe the problem conversationally and let Claude be your thinking partner. It asks clarifying questions, surfaces tradeoffs you haven't considered, suggests approaches, and pokes holes in your plan — so by the time you say "ok, build it," both of you know exactly what to build and why.
Paste error messages with "why did this fail?" instead of "fix this" to get Claude to diagnose the root cause before applying a fix.
When you need Claude to make changes in one area without affecting another, add negative constraints — "fix the bug but don't change the public API", "refactor the internals but don't create new files", or "update the logic but don't modify any tests." Explicit exclusions prevent Claude from making well-intentioned changes you'll have to undo.