Home › Coding › Generate a regex or SQL query: how to do it with AI
Generate a regex or SQL query: how to do it with AI
From official vendor pages; selected tasks also include dated logged-out free-plan trials. Report an error.
Beginner Risk: Low 💻 Coding
Describe the text you want to match or the data you want to pull, and an assistant writes the regular expression or SQL query and explains each part. The step that makes it safe is testing it on real sample rows, including the awkward ones, before it goes anywhere near production.
Who it’s for: Analysts, developers, marketers cleaning lists and anyone who writes the occasional spreadsheet filter, log search or database query.
Why low risk: A pattern or query can look right and still miss edge cases, match too much, or run slowly on a large table.
ChatGPT has the highest score on our published rubric (94/100) of the 5 tools listed for this task. Its free plan includes limited data analysis (verified on its pricing page), enough to test a pattern on a few rows.
Step by step
- Write down 5 to 10 example inputs: some that should match and some that shouldn't.
- Say which flavour you need (for example PostgreSQL, MySQL, Python re or JavaScript), because syntax differs.
- For SQL, paste the table names and column names (not the data) so the query uses the real schema.
- Ask for the pattern or query plus a plain-English explanation of each part.
- Run it on your examples and a copy of real data, then paste back anything it got wrong.
- For queries that change data (UPDATE, DELETE), run a SELECT with the same WHERE clause first.
AI prompts to generate a regex or SQL query
Replace the [brackets] with your details. See all prompt examples
Write a [regex flavour / SQL dialect] that [describe the goal]. These should match: [examples]. These should NOT match: [examples]. Explain each part in one line.
Explain this [regex / SQL query] line by line in plain English, then tell me which inputs it would get wrong: [paste]
Here is my [pattern / query] and the result on my sample. List 10 tricky inputs or data situations (empty values, accents, time zones, duplicates) it might mishandle, and give a fixed version.
Illustrative example
You paste
Goal: match Canadian postal codes like "K1A 0B1" or "k1a0b1".
A good answer looks like
Pattern: ^[A-Za-z]\d[A-Za-z] ?\d[A-Za-z]\d$ Explanation: letter, digit, letter, optional space, digit, letter, digit. It doesn't exclude letters Canada Post never uses; ask for a stricter version if that matters.
Illustrative example written for this guide, not output captured from a specific tool.
Which tool to pick, and why
- Claude (incl. Claude Code): Free plan verified for files and code execution, so it can run the pattern against a sample file you upload. Visit official website ↗
- ChatGPT (incl. Work & Codex): Its free plan includes limited data analysis (verified on its pricing page), enough to test a pattern on a few rows. Free plan can do this Visit official website ↗
- GitHub Copilot: Free plan: 2,000 completions a month plus limited chat and agent usage inside your editor, where the query or pattern will live. Free plan can do this Visit official website ↗
- Cursor: An AI code editor whose agent can read your whole project. Free plan verified with a limited allowance. Free plan can do this Visit official website ↗
- Mistral Vibe (formerly Le Chat): Free plan includes limited messages, enough for a few quick patterns. Free plan can do this Visit official website ↗
Reasons come from the capabilities, free-plan details and prices we've checked on official pages (separate from any dated trial above). Sources: claude.com/pricing · chatgpt.com/pricing · github.com/features/copilot/plans · cursor.com/pricing · mistral.ai/pricing
Common mistakes
- Testing only on the examples you gave the AI.
- Forgetting to say which regex flavour or SQL dialect you use.
- Running an UPDATE or DELETE before checking the same WHERE clause with SELECT.
- Pasting real customer rows when column names would do.
Privacy tip
Share table and column names, not real rows. If you need sample data, make up a few fake rows with the same shape.
Claude (incl. Claude Code) setting · ChatGPT (incl. Work & Codex) setting · GitHub Copilot setting · Sources: privacy.claude.com/en/articles/12109829-how-do-i-change-my-model-improvement-privacy-settings · help.openai.com/en/articles/7730893-data-controls-in-chatgpt · github.com/features/copilot/plans