Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
gm

Test String

Substitution

Processing...

Code Generator

Generated Code

use strict; my $str = '01 AC ACRE Sim 02 AL ALAGOAS Não 03 AP AMAPÁ Sim 04 AM AMAZONAS Sim 05 BA BAHIA Não 06 CE CEARÁ Não 07 DF DISTRITO FEDERAL Não 08 ES ESPÍRITO SANTO Não 10 GO GOIÁS Não 12 MA MARANHÃO Não 13 MT MATO GROSSO Não 28 MS MATO GROSSO DO SUL Não 14 MG MINAS GERAIS Não 15 PA PARÁ Não 16 PB PARAÍBA Não 17 PR PARANÁ Não 18 PE PERNAMBUCO Não 19 PI PIAUÍ Não 22 RJ RIO DE JANEIRO Não 20 RN RIO GRANDE DO NORTE Não 21 RS RIO GRANDE DO SUL Não 23 RO RONDÔNIA Sim 24 RR RORAIMA Sim 25 SC SANTA CATARINA Não 26 SP SÃO PAULO Não 27 SE SERGIPE Não 29 TO TOCANTINS Não '; my $regex = qr/(\d\d) (\w\w) (.*?) (Sim|Não)/mp; my $subst = 'case "\\2": _UF = "\\1";\\n'; my $result = $str =~ s/$regex/$subst/rg; print "The result of the substitution is' $result\n";

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html