Skip to content
Back to AI Coding

Regex Generation

Generate and explain regex from intent + test pairs; never ship unexplained regex.

Code GenerationIntermediate

Why

GitHub's 2024 Octoverse reported a 59% surge in contributions to generative AI projects and a 98% increase in AI projects overall on the platform. Regex is write-once-read-never because it's opaque.

Generating it from intent + test pairs, and requiring an explanation, makes it auditable and editable — and the tests prove it matches intent.

When

Use whenever you need a non-trivial regex. Skip for trivial literal matches.

How

1. State the intent in words ("match a slug: lowercase letters, digits, hyphens; not all hyphens"). 2. Give 3+ match/no-match examples. 3. Ask: "write the regex + explain each group; verify against the examples". 4. Add the examples as unit tests. 5. Comment the regex in code with the intent.

Example

Intent: "slug: [a-z0-9-]+, 3-63 chars, not starting/ending with -" Examples: "abc-1" ✓, "-abc" ✗, "abc-" ✗, "a" ✗ (too short), "ABC" ✗ Regex: /^(?=[a-z0-9])[a-z0-9-]{3,63}(?<=[a-z0-9])$/ Tests assert each example; comment in code restates the intent.

Common Problems

  • ⚠Generated code fails to compile: "SyntaxError: Unexpected token" → ask the model to output a complete file with correct imports; verify with `npx tsc --noEmit`.
  • ⚠Error: Hallucinated API: model calls a function that doesn't exist → cross-check against the actual library docs and pin the version in package.json.
  • ⚠Error: Cannot find module 'xxx' → `npm install xxx`; generated code often imports packages not yet in the project.

Best Practices

  • ✓Always require an explanation and intent comment for non-trivial regex.
  • ✓Pin regex with match/no-match unit tests.
  • ✓Measure the outcome with a concrete metric (time saved, defect rate, test coverage) so the practice can be justified and improved.

FAQ

What is Regex Generation?

Generate and explain regex from intent + test pairs; never ship unexplained regex. It helps developers quickly grasp the core value and use cases of Regex Generation.

How do I get started with Regex Generation?

To get started with Regex Generation, focus on these points first: State the intent in words ("match a slug: lowercase letters, digits, hyphens; not all hyphens").

What are common mistakes with Regex Generation?

Common mistakes when using Regex Generation include: Generated code fails to compile: "SyntaxError: Unexpected token" → ask the model to output a complete file with correct imports; verify with `npx tsc --noEmit`.; Error: Hallucinated API: model calls a function that doesn't exist → cross-check against the actual library docs and pin the version in package.json.; Error: Cannot find module 'xxx' → `npm install xxx`; generated code often imports packages not yet in the project..

What are the best practices for Regex Generation?

Best practices for Regex Generation include: Always require an explanation and intent comment for non-trivial regex.; Pin regex with match/no-match unit tests.; Measure the outcome with a concrete metric (time saved, defect rate, test coverage) so the practice can be justified and improved..

How do I learn more about Regex Generation?

Start with the official docs, then search real-world examples on GitHub. Validate patterns in a small scratch repo before applying them to production code.