Generate Perl code from a regex
Drop a pattern in, pick flags, and get a Perl snippet — qr// precompile, =~ match, m// (first / all), s/// replace, split — with /g, /i, /m, /s, /x modifiers.
#!/usr/bin/perl
use strict;
use warnings;
my $input = 'Reach alice@example.com or bob@test.org.';
my $re = qr/\\b\\w+@\\w+\\.\\w+\\b/;
my $is_match = ($input =~ $re) ? 1 : 0;
my ($first) = $input =~ /$re/;
my @all = $input =~ /$re/g;
(my $replaced = $input) =~ s/\\b\\w+@\\w+\\.\\w+\\b/[email]/g;
my @parts = split /$re/, $input;
print "is_match=$is_match\n";
print "first=", (defined $first ? $first : "undef"), "\n";
print "all=[", join(", ", @all), "]\n";
print "replaced=$replaced\n";
print "parts=[", join(", ", @parts), "]\n";How to use a regex in Perl
Perl invented the syntax. Patterns live in qr/.../, m/.../ or s/.../.../ — bound to a string with =~.
- 01
Type a pattern
Perl's m// uses the same regex syntax as JS for the most part. Forward slashes are auto-escaped to play nice with / delimiters.
Patterns are parsed with the JavaScript RegExp constructor, so syntax errors surface immediately and engine quirks like Unicode property escapes (\p{L}) require the u flag.
patternECMAScriptliteralregexinput · literalpattern · regexqr/\b\w+@\w+\b/
loaded · ECMAScripttarget · ready - 02
Tune flags
JS i/m/s/x/u map directly to Perl modifier letters. Append them after the closing delimiter — m/pat/im or qr/pat/im.
Each flag character mirrors the ECMAScript literal: g enables iteration, i folds case, m anchors per line, s lets . match newlines, u unlocks proper Unicode, y is sticky, and d emits indices.
flagssampleg · i · mliveconfig · optionsflags · flagsqr/pat/im
flags applied · livelive · preview - 03
Read the snippet
Includes qr precompile, =~ test (boolean), capture (first), /g list (all), s/// substitute, and split.
The generator rewrites the regex for the target language — handling string-literal escaping, raw-string prefixes, and engine-specific syntax tweaks.
codegentargetsnippetcopyrun · outputpreview · code$str =~ s/…/…/g
snippet · livecode · ready - 04
Copy code
One click copies the full script — save as .pl and run with perl script.pl.
Copy writes via navigator.clipboard.writeText, so nothing transits the network and the output is byte-identical to what you see on screen.
copyexportclipboardblobexport · exportedready · savedCopied
ready to shipsuccess
Regex patterns that don't melt down
Use named groups
(?<year>\d{4}) reads better than $1 and survives reordering. Supported in ECMAScript 2018+.
Anchor strictly
Use ^/$ with the m flag when matching whole lines. Without anchors, partial matches sneak through.
Avoid catastrophic backtracking
Nested quantifiers like (a+)+ can explode. Run patterns through ReDoS Detector first.
Escape every literal
Backslash any meta character you want literally: \.\$\^\*\+\?\(\)\[\]\{\}\|\\.
Test edge cases
Empty string, single character, unicode, the maximum length you expect — write fixtures for each.
Patterns aren’t parsers
Don’t parse HTML, JSON, or balanced parens with regex. Use a proper parser when the grammar is recursive.
When to generate Perl regex code
Skip the boilerplate when wiring a pattern into a sysadmin script, a CGI handler, or a CPAN module.
Sysadmin one-liners
perl -ne 'print if /pat/' files — fastest line filter on Unix. Compiles through the JavaScript RegExp constructor — engine quirks (Unicode property escapes, sticky matching, named groups) work when their flags are on. Error messages cite the exact position of any syntax issue.
ReDoS-safeLog scraping
Capture every IP / timestamp / status code from access logs. Pairs with the ReDoS detector to catch catastrophic backtracking before you deploy. A pattern that handles 1k-character input fine can become a denial-of-service vector on attacker-controlled 1M-character input.
ECMA-compliantData munging
Transform CSV columns with /e modifier and an inline expression. Output exports as JavaScript, Python re, Java util.regex, .NET System.Text.RegularExpressions, PCRE, and POSIX — language quirks are highlighted on conversion so you don't ship a JS-only pattern to a Go service.
spec-gradeCPAN modules
Embed regex in a Mojolicious or Dancer route handler. Capture groups (named or numbered) round-trip cleanly into replacement strings, JSON output, or CSV extraction without re-typing. Backreferences ($1, ${name}) preserve semantics across languages.
cross-engineBuild scripts
Apply patterns in Makefile recipes via perl -pi -e. Test against edge cases — empty input, single character, max length, multibyte boundaries — to catch off-by-one and zero-length match bugs before they reach production.
test-readyMigration shims
Translate legacy formats during a database migration. All matching runs client-side so secret patterns never leave the browser. Useful when the pattern itself is sensitive (PII regexes, internal data formats, security filters).
production-readyFrequently asked questions
Something missing?
Tell us what you need. We ship the top requests every two weeks.