Regular Expressions 101

Save & Manage Regex

  • Current Version: 3
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
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
Processing...

Test String

Substitution
Processing...

Code Generator

Generated Code

$re = '/(?=((?=(?|(?!.*+\2)()(1?)|(?!.*+\3)(2?)()|(1)|()(2))).)+).*\K((?!.*+\2)|(?!.*+\3)1|(?=.*+\2\3)2)(.*)|(..)\6*\K.?/m'; $str = '111 112 121 122 211 212 221 222 1111 1112 1121 1122 1211 1212 1221 1222 2111 2112 2121 2122 2211 2212 2221 2222 11111 11112 11121 11122 11211 11212 11221 11222 12111 12112 12121 12122 12211 12212 12221 12222 21111 21112 21121 21122 21211 21212 21221 21222 22111 22112 22121 22122 22211 22212 22221 22222 111111 111112 111121 111122 111211 111212 111221 111222 112111 112112 112121 112122 112211 112212 112221 112222 121111 121112 121121 121122 121211 121212 121221 121222 122111 122112 122121 122122 122211 122212 122221 122222 211111 211112 211121 211122 211211 211212 211221 211222 212111 212112 212121 212122 212211 212212 212221 212222 221111 221112 221121 221122 221211 221212 221221 221222 222111 222112 222121 222122 222211 222212 222221 222222 1111111 1111112 1111121 1111122 1111211 1111212 1111221 1111222 1112111 1112112 1112121 1112122 1112211 1112212 1112221 1112222 1121111 1121112 1121121 1121122 1121211 1121212 1121221 1121222 1122111 1122112 1122121 1122122 1122211 1122212 1122221 1122222 1211111 1211112 1211121 1211122 1211211 1211212 1211221 1211222 1212111 1212112 1212121 1212122 1212211 1212212 1212221 1212222 1221111 1221112 1221121 1221122 1221211 1221212 1221221 1221222 1222111 1222112 1222121 1222122 1222211 1222212 1222221 1222222 2111111 2111112 2111121 2111122 2111211 2111212 2111221 2111222 2112111 2112112 2112121 2112122 2112211 2112212 2112221 2112222 2121111 2121112 2121121 2121122 2121211 2121212 2121221 2121222 2122111 2122112 2122121 2122122 2122211 2122212 2122221 2122222 2211111 2211112 2211121 2211122 2211211 2211212 2211221 2211222 2212111 2212112 2212121 2212122 2212211 2212212 2212221 2212222 2221111 2221112 2221121 2221122 2221211 2221212 2221221 2221222 2222111 2222112 2222121 2222122 2222211 2222212 2222221 2222222 11111111 111111111 1111111111 11111111111 111111111111 1111111111111 11111111111111 22222222 222222222 2222222222 22222222222 222222222222 2222222222222 22222222222222'; $subst = "$5 ${6:-3}"; $result = preg_replace($re, $subst, $str); echo "The result of the substitution is ".$result;

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php