Coding
AI Prompt for Regex Data Extraction
Extract structured data with this AI prompt for regex: named capture groups, complete extraction code, malformed-line handling, and output demos.
The prompt
Role: You are a data extraction specialist who pulls structured data out of messy text with regex. Context: Regex flavor / language: [FLAVOR]. Source text: [SOURCE_DESCRIPTION, e.g. nginx access logs, email bodies]. Fields to extract: [FIELDS, e.g. IP address, timestamp, status code] Sample lines: [SAMPLE_LINES] Desired output: [OUTPUT_FORMAT, e.g. list of dicts, CSV rows] Task: 1) Write the regex using NAMED capture groups (one per field) so the code is self-documenting. 2) Write the complete extraction code in [FLAVOR]'s language: read lines, apply regex, build [OUTPUT_FORMAT]. 3) Handle malformed lines per this policy: [BAD_LINE_POLICY: skip with warning / collect separately]. 4) Demonstrate on the sample lines showing extracted output. Format: Regex first with group explanations, then the full extraction code, then the demonstration. Ask the AI to add a per-field extraction success rate to the output summary, so you can see at a glance which fields your pattern struggles with on real data. Constraints: Use named groups, not positional ones. Never crash on a malformed line — follow [BAD_LINE_POLICY]. Prefer readability; this code will be maintained.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[FLAVOR] [SOURCE_DESCRIPTION] [FIELDS] [SAMPLE_LINES] [OUTPUT_FORMAT] [BAD_LINE_POLICY]
Example input
FLAVOR=Python, SOURCE=server logs, EXTRACT=IP address, timestamp (ISO), and HTTP status code from each line, SAMPLE=3 real log lines, OUTPUT=list of dicts
How to use this prompt
Paste 3-5 real sample lines — synthetic examples miss the weirdness of real logs. Ask for named groups; they make the extracted data self-documenting.
Tips for the best results
- Named groups double as documentation — insist on them always.
- Ask for a count of skipped malformed lines in the output for data-quality visibility.
- For huge files, ask for the streaming version that processes line by line.
Frequently asked questions
Can it write the whole extraction script?
Yes — it writes the full extraction code with named groups, which beats a bare pattern for real pipelines.
What if some lines do not match at all?
Set the bad-line policy in the prompt: skip with a warning, or collect failures separately for inspection. Never let one bad line kill the whole run.