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

/
/

Test String

Code Generator

Generated Code

$re = '/^(?:[^ ]*, )*?(?<client_ip>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<uri>[^\"]*?)(?: +\S*)?)?" "(?<body>.*?)" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")? (?<request_time>[^ ]*) (?<upstream_response_time>.*)$/'; $str = '140.207.54.75, 100.97.90.103, 10.117.43.51 - - [24/Jun/2016:05:32:02 +0800] "POST /weixin/notify/open/authorize?signature=2ac1ca77cf259bc2b234e3261e320db698c53a9c&timestamp=1466717523&nonce=1035843400&encrypt_type=aes&msg_signature=2712811c2c6cf0cacdfb65461ab4c23ec36a3c61 HTTP/1.0" "<xml>\\x0A <AppId><![CDATA[wxe484f12197737223]]></AppId>\\x0A <Encrypt><![CDATA[By8VnMuxv0R05S8Zl4+eY5Nq5nzqjNcTN8ljzfIOBM/GSzYMEPXwNck62DjFqnSbOkHK0xZ4uLJkc09Bnomr4Q/Zdt98YGNcdXmW4lDA8rQPfJU97hRfM5EwWGUI1n2EOgk8rSBVNwYE08DMdtXy6NVtPEi3JVktUlq0X5DkMhQg+YchJTAeiw+zbXoqYjWWX+prSDPXFArLu+6jzKliB8i+B9R2uaqtx+f2ceRY2vPoVP4p9xg2+ViOCM7n3rA8hjvk9Qf2pdeAvLsJxon/NTRkSAiUDKYwEwT5zF5sJ7S0nem3PyXHtiNczIDeNIIhl0lbsfY4muVuDx0I0GFp7PH/mJM9OdpJSarE6znxTAuij151NsmiehZsCAEQqKhbiVCCyvvdHAmWvZn1UZ86XSF5d0r1HBPeUYegsHfuj7lOpkalB65Br3DVDEkPjiCljwlcx7R5rAutvRHzBx2Y/A==]]></Encrypt>\\x0A</xml>\\x0A" 200 7 "-" "Mozilla/4.0" 0.008 0.007'; preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 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