perl · qr · =~ · m// s/// · local

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.

Regex → Perl
//g
Perl
#!/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";
Quick Start

How to use a regex in Perl

Perl invented the syntax. Patterns live in qr/.../, m/.../ or s/.../.../ — bound to a string with =~.

  1. 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.

    patternECMAScriptliteralregex
    input · literalpattern · regex
    qr/\b\w+@\w+\b/
    loaded · ECMAScripttarget · ready
  2. 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 · mlive
    config · optionsflags · flags
    qr/pat/im
    flags applied · livelive · preview
  3. 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.

    codegentargetsnippetcopy
    run · outputpreview · code
    $str =~ s/…/…/g
    snippet · livecode · ready
  4. 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.

    copyexportclipboardblob
    export · exportedready · saved
    Copied
    ready to shipsuccess
Pro tips

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&rsquo;t parsers

Don’t parse HTML, JSON, or balanced parens with regex. Use a proper parser when the grammar is recursive.

Use Cases

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-safe

Log 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-compliant

Data 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-grade

CPAN 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-engine

Build 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-ready

Migration 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-ready
FAQ

Frequently asked questions

qr// precompiles a regex into a Regexp object you can store, pass around, and reuse. m// matches inline. Use qr/ when you'll match more than once; use m/ for one-shots.
It binds a regex operation to a string. Without it, m// / s// operates on $_ (the default variable). $str =~ /pat/ tests whether the pattern matches $str.
Extended mode — lets you add whitespace and # comments to your pattern for readability. The whitespace and comments are stripped before matching.
Yes. (?<name>...) is supported since Perl 5.10. Access via $+{name} after a successful match. Much clearer than positional $1, $2, etc.
No. The code is generated entirely in your browser.
Suggest a tool

Something missing?

Tell us what you need. We ship the top requests every two weeks.

Sent straight to the maintainers · no account needed
47 in queue·8 shipped this month
Request queued. Thanks!