Regex Code Generator

Choose a target language and supply the pattern. The generator handles the code wrapper; review regex features against the target engine.

Runs locally in your browser

How to turn a regex pattern into a runnable code example

Type a regular expression, choose one of the seven target languages and press Generate code. The pane returns a short program that compiles the pattern with that language’s own regex API and tests it against a sample string.

The snippet is assembled in the page: generating the examples sent no network request in testing, so the pattern never leaves the browser and the tool keeps working after the connection drops.

  1. Type or paste the pattern into the field, for example \d{6}, or start from one of the eleven Quick patterns — email address, HTTP URL, IPv4 address, UUID, ISO date, 24-hour time, hex colour, integer, decimal number, URL slug and semantic version. A quick pattern only fills the field; nothing runs until you press the button.
  2. Choose the target language: JavaScript, PHP, Go, Java, Ruby, Python or C#.
  3. Press Generate code. The example appears in the pane below; Copy puts the visible snippet on the clipboard, and Clear empties both the field and the pane.

What the generated example contains

What each target emits

JavaScript: new RegExp("…") plus pattern.test(text). PHP: preg_match('~…~', $text, $matches) with a var_dump of the result, and the ~ delimiter escaped inside the pattern so that a literal tilde keeps its meaning. Go: regexp.Compile with MatchString, and the compile error routed through panic. Java: Pattern.compile and matcher(text).find(). Ruby: Regexp.new('…') and match?. Python: re.compile, search and bool(). C#: a verbatim @"…" string with Regex.IsMatch(text, pattern).

Every example keeps the sample text in a variable called text ($text in PHP) holding an empty string, and the pattern is escaped for that language’s string literal — backslashes, the quoting character, and \r or \n where the language needs them — so patterns such as say "hi", a\~b or a~b reach the target engine unchanged. Each snippet was executed against matching and non-matching input for those cases and returned the expected result.

Check before you paste the snippet into your project

The samples test an empty string, so most of them print a negative result until you replace text with your own input. Nothing is validated: a pattern that cannot compile — a lone backslash, an unclosed group — is copied exactly as typed and fails in the target language, and flags written as part of the pattern (/abc/i) stay literal text instead of becoming that language’s own options.

PHP runs on PCRE with the ~ delimiter, and PCRE has no \uXXXX escape: for a character such as é use \x{00E9} with a /u pattern. Go compiles with regexp, which rejects lookaround and backreferences, so the generated program panics on patterns that JavaScript or Python accept.

The same pattern is not the same everywhere

The tool copies the pattern text; it does not translate it between engines. Measured with the same pattern in three engines: ^b matches inside "a\nb" in Ruby, where ^ and $ anchor to lines, and does not match in JavaScript, Python or PHP. \d matches the Arabic-Indic digit ٣ in Python, and in PHP only with the /u modifier, while Ruby and JavaScript keep \d to the ASCII digits 0–9. Test the pattern in the target language before shipping it.

Recent tools: