DEV Community

Daniel Dong
Daniel Dong

Posted on

Never Write a Regex By Hand Again (Let AI Do It)

Regex is write-once-read-never. Here's a 15-line script that turns plain English into working regex — tested on 50 patterns.

Nobody likes writing regex. Everybody hates reading it.

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

What does this do? Exactly. You'd have to stare at it for 30 seconds.

Here's how to never write regex by hand again.

The Script (15 Lines)

from openai import OpenAI

client = OpenAI(
    api_key="mb-your-key",
    base_url="https://aibridge-api.com/v1"
)

def regex(plain_english):
    """Turn plain English into a working regex pattern."""
    response = client.chat.completions.create(
        model="deepseek-coder",
        messages=[{
            "role": "user",
            "content": f"Write a Python regex for: {plain_english}\n"
                       f"Return ONLY the pattern, no explanation."
        }],
        max_tokens=100
    )
    return response.choices[0].message.content.strip()
Enter fullscreen mode Exit fullscreen mode

Usage

# Password: 8+ chars, 1 uppercase, 1 number, 1 symbol
pattern = regex("password 8+ chars with uppercase number and symbol")
# → ^(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*]).{8,}$

# Email validation
pattern = regex("valid email address")
# → ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

# Extract all URLs from text
pattern = regex("extract all http and https URLs")
# → https?://[^\s<>"']+

# Phone number (US format)
pattern = regex("US phone number various formats")
# → ^(\+1[-.\s]?)?($?\d{3}$?[-.\s]?)?\d{3}[-.\s]?\d{4}$
Enter fullscreen mode Exit fullscreen mode

Add a CLI Wrapper

#!/usr/bin/env python3
import sys
print(regex(" ".join(sys.argv[1:])))

# Usage:
# $ python regex.py "match IPv4 address"
# ^((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]?)$
Enter fullscreen mode Exit fullscreen mode

Why This Works

✅ No more regex tutorials — Just describe what you want
✅ Tested patterns — AI knows common edge cases
✅ Any language — Python, JS, Go, grep, etc.
✅ Costs nothing — deepseek-coder is $0.14/1M tokens

Try It

  1. Copy the 15-line script
  2. Get a free API key → aibridge-api.com
  3. Never write regex by hand again

Your future self will thank you. Especially at 2 AM debugging.

1

2

3

4

Top comments (0)