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 = '/Host: (?:(?:(?:22[0-3]|2[01]\d|1[79][013-9]|16[0-8]|12[0-68-9]|1[013-58]\d|[2-9]\d|1?[1-9])\.(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d))|(?:172\.(?:25[0-5]|2[0-4]\d|1\d{2}|[4-9]\d|3[2-9]|1[0-5]|\d))|(?:192\.(?:25[0-5]|2[0-4]\d|16[0-79]|1[0-57-9][0-9]|[1-9]\d|[0-9]))|(?:169\.(?:25[0-35]|2[0-4]\d|1\d{2}|[1-9]?\d)))(?:\.(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)){2}(?::(?:6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}))?[\r\n]/m'; $str = '------------------------------------- Testing Criteria (valid and invalid): ------------------------------------- Host: 212.83.166.45:8080 Host: 87.106.149.74:443 Host: 169.172.234.14:7777 Host: 0.127.134.10:65535 Host: 127.0.0.1:23 Host: 8.8.8.8:65536 Host: 224.0.0.9 Host: 172.128.244.1 Host: 4.22.222.1 Host: 10.11.112.13 Host: 192.155.172.13:1 Host: 172.16.0.1 Host: 172.31.255.255 Host: 192.168.123.213 Host: 173.245.2.23 Host: 192.172.21.5 Host: 192.256.75.1 Host: 8.8.4.4:80 Host: 169.254.23.162 Host: 187.135.2.231 ------------------------------------- Scratch Notes: ------------------------------------- Combine them: --------- Host: (?: (?:(?:22[0-3]|2[01]\\d|1[79][013-9]|16[0-8]|12[0-68-9]|1[013-58]\\d|[2-9]\\d|1?[1-9])\\.(?:25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d))| (?:172\\.(?:25[0-5]|2[0-4]\\d|1\\d{2}|[4-9]\\d|3[2-9]|1[0-5]|\\d))| (?:192\\.(?:25[0-5]|2[0-4]\\d|16[0-79]|1[0-57-9][0-9]|[1-9]\\d|\\d))| (?:169\\.(?:25[0-35]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)) ) (?:\\.(?:25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)){2} --------- Match 0-255: (?:\\.(?:25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)){2,3} Exclude Class A, D, E Address Ranges: 0 10 127 224 255 Host: (?:(?:22[0-3]|2[01]\\d|1[013-9]\\d|[1-9]\\d|[1-9])\\.) -- [v1] << Note: Forgot, since Class B address ranges will be specified, they need to be excluded in this segment as well >> 169 172 192 Host: (?:(?:22[0-3]|2[01]\\d|1[79][013-9]|16[0-8]|12[0-68-9]|1[013-58]\\d|[2-9]\\d|1?[1-9])\\.) -- [v2] Exclude Class B Address Ranges: 172.16-31 (?:172\\.(?:25[0-5]|2[0-4]\\d|1\\d{2}|[4-9]\\d|3[2-9]|1[0-5]|\\d)) 192.168 (?:192\\.(?:25[0-5]|2[0-4]\\d|16[0-79]|1[0-57-9][0-9]|[1-9]\\d|\\d)) 169.254 (?:169\\.(?:25[0-35]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)) ------------------------------------- What this PCRE excludes: ------------------------------------- 127.X.X.X 10.X.X.X 169.254.X.X 172.16.X.X 172.31.X.X 192.168.X.X 224.X.X.X (to) 255.X.X.X ------------------------------------- Now to match valid ports: ------------------------------------- 0-65535 (?::(?:6553[0-5]|655[0-2]\\d|65[0-4]\\d{2}|6[0-4]\\d{3}|[1-5]\\d{4}|[1-9]\\d{0,3}))?[\\r\\n]'; 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