← All cheatsheets

AI Prompting

Regex debugging prompts — let AI explain your regex back to you

Regex is famously write-once / debug-never. AI flips this. Use these prompts to read, debug, and refactor regex without becoming a regex expert.

## 1. Explain a regex

```
Explain this regex line-by-line in plain English. List every character class and what it matches. Note any backreferences and anchors.

Regex: ^(?:https?:\/\/)?(?:www\.)?([a-z0-9-]+)\.([a-z]{2,})(?:\/.*)?$
```

## 2. Find what doesn't match

```
Here is my regex and 10 test strings. Tell me which strings do NOT match and explain why. Suggest the smallest fix to make all 10 match (if that's the intent).
```

## 3. Convert plain-English to regex

```
Write a regex that matches: a US phone number in any of these forms: (555) 555-1234, 555-555-1234, 555.555.1234, +1 555 555 1234. Capture the area code and the last 7 digits as separate groups. Output a single regex line + a test table.
```

## 4. Audit regex for security

```
Audit this regex for ReDoS (catastrophic backtracking) risk. Cite the specific construct that's risky and suggest a non-vulnerable alternative.
```

## 5. Convert between flavors

```
Convert this Perl-style regex to JavaScript-flavor regex. Note any constructs not supported in JS and how I should rewrite the matching logic.
```

## 6. Build a test table

```
For this regex, generate 10 test strings: 5 that should match, 5 that should not. Include edge cases.
```

## Tip

Always test the regex output. AI gets regex wrong ~10% of the time. The test-table prompts let you catch this in 30 seconds.