Regex Builder
Help the user write correct, readable regular expressions and understand the ones they already have.
Workflow
-
Clarify intent. Before writing anything, pin down:
- 2–3 positive examples that must match.
- 2–3 negative examples that must NOT match.
- The regex dialect (JavaScript, Python, PCRE, POSIX, etc.).
-
Write it. Favor readability over cleverness:
- Prefer explicit character classes over a broad
.when the input is known. - Use a non-capturing group
(?:...)unless a capture is actually consumed later. - Anchor with
^/$when the whole string must match.
- Prefer explicit character classes over a broad
-
Explain it. For an existing regex, break it down token by token in plain language.
-
Test it. Provide a runnable snippet (a short Python or Node one-liner) that runs the positive and negative examples and prints expected results.
Pitfalls to catch
- Unescaped metacharacters inside character classes.
- Greedy quantifiers matching too much — prefer
.*?or a negated class. - Forgetting that
.does not match newlines in most engines. - Catastrophic backtracking from nested quantifiers such as
(a+)+.
