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

Code Generator

Generated Code

$re = '/^(PO BOX|BOX)+\s*[A-Z]?[0-9]+[A-Z]*\s.*$/m'; $str = 'BOX ELDER CT PO BOX 111 PO BOX 191 39TH ST 6TH ST 4TH AVE 5TH AVE 5TH ST 2ND AVE 15TH AVE BOX ELDER DR PO BOX 297 PO BOX 42 PO BOX 18 PO BOX 16 PO BOX 115A PO BOX 245 PO BOX 131 PO BOX 254 PO BOX 254 PO BOX 49B PO BOX 373 PO BOX 178 PO BOX 245 PO BOX 51 PO BOX 400 PO BOX 25 PO BOX 25 PO BOX 7 PO BOX 97 PO BOX 363 PO BOX 482 PO BOX 24 PO BOX 55 PO BOX 204 PO BOX 482 PO BOX 331 PO BOX 99 PO BOX 204 PO BOX 224C BOX 246 BOX 2005 BOX 431 ATLAN & CLEV BOX 384 BOX 22 BOX 247 BOX 212 BOX 85 PO BOX 192 PO BOX 192 BOX 232 BOX 28 PO BOX A73 PO BOX 53 BOX 4 PO BOX 266 BOX 154 BOX 261 BOX 82 BOX ELDER LN BOX 101 PO BOX 76 EAST MARKET '; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

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