piapia123
en

Regex Code Generator

Web

Everything runs locally in your browser — nothing is uploaded

Input
Drop a file here to load its contents
Output

Enter a regular expression, choose a language, and get four ready snippets: match, find all, replace and test. The point of this tool is how many backslashes to write. The same `\d+` needs a different number of them depending on the language, and getting it wrong raises nothing: · In JavaScript `"\d"` is `"d"` — an invalid escape in a string is silently dropped, so your pattern quietly becomes `d` and matches nothing, with no warning at all. Write `"\\d"`, or use a regex literal `/…/`. · Python uses a raw string `r"…"` so no doubling is needed; a plain string now raises a DeprecationWarning. · Java and C# double-quoted strings treat the backslash as an escape, so you must write `\\d`. · In a PHP single-quoted string only `\\` and `\'` are escapes, so `'/\d/'` is correct — doubling there is the mistake. · Go uses a backtick raw string, which also needs no doubling. ⚠️ A pattern that does not compile is rejected here rather than turned into snippets that fail in your project, where the error message is far less clear. ⚠️ The `$1` / `\1` in a replacement is a group reference and its syntax differs (`\1` in Python, mostly `$1` elsewhere); a note says so.

Features

  • Six languages: JavaScript, Python, Java, Go, PHP, C#
  • ⚠️ Each language gets its own correct escaping, not the same string reused
  • Four snippets: match, find all, replace, test
  • Snippets are complete and runnable (with imports and declarations), not a bare expression
  • Flags are translated per language (`re.IGNORECASE`, `Pattern.CASE_INSENSITIVE`, `RegexOptions.IgnoreCase`)
  • ⚠️ A pattern that does not compile is reported immediately
  • The generated code does not change with the interface language
  • Computed locally

How to use

  1. Enter a regular expression (without the surrounding slashes)
  2. Choose the target language; add flags and a replacement if needed
  3. Four snippets for that language appear immediately
  4. Before copying, look at the “Inside a string literal” row — that is where mistakes happen

FAQ

Why does "\d" not work in JavaScript?
Because an invalid escape inside a string is silently dropped: `"\d"` in JS is just `"d"`, so your pattern ends up matching the letter d. It is the hardest kind of bug to find — no error, no warning, it simply never matches. Two fixes: write `"\\d"` (doubled), or use a regex literal `/\d/` (no doubling inside a literal, which is what the JavaScript snippets here use). Java and C# double-quoted strings behave the same way, except they fail at compile time, which is safer.
Why no doubling in a PHP single-quoted string?
Because only `\\` and `\'` are escape sequences in a PHP single-quoted string; every other backslash is preserved as written. So the `\d` in `'/\d/'` reaches the regex engine intact. PHP double-quoted strings, by contrast, treat `\d` as an escape (the result is still d, but a warning is emitted), so doubling is needed there. Two quoting rules in one language is exactly why this tool gives per-language snippets.
Can I paste these straight into a project?
Yes — the snippets are complete: they include the import/using and the variable declarations. A few things are yours to adjust: the variable name (`text` is your own string), and the group-reference syntax in a replacement (Python uses `\1`, Go `$1` or the safer `${1}`, most others `$1`). Note also that only the shared flags (i / m / s) are translated — `g` is a JavaScript-specific idea, and other languages express “keep finding matches” as a loop, which is why the find-all snippet for Java is `while (matcher.find())`. The `y` (sticky) flag is JavaScript-only too, and picking it is reported as ignored.

Related tools