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 = '/(?<src_ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s\D{1,5}(?<date_time>[^ ]+)\s(?<GMT_offset>[^ ]+)\s(?<action>[^ ]+.+a=)(?<hash_url>[^ ]+)\s(?<protocol>[a-zA-Z]+)[^ ]+\s(?<code>\d{1,3})\s(?<octet>[^ ]+)\s(?<ratio>[^ ]+)\W(?<percent_comp>[^ ]+)\W(?<dst_url>[^ ]+)\s(?<user_agent>[^ ]+.+)/'; $str = '80.254.146.36 - - [11/Jun/2015:11:04:10 +0000] "GET /alert/process?a=-3-kj8UgHY54dtgX8mZCfPUCRkiu-r_Kh1hnSqbe40rOjxSzUCVGJLfF_PiDhvXVCFsI3a8RMB5tiVZGzG_A0LowA&b=K1ky5h_rXTuqI2kqaBwFai5qnz5xw_pL_RrBcgfqJok-Rrw7d7i2aziQR53ekX-XFPMy2cECsNi1VNW5nIav_sB7MR1WAKQ2pUIc8ypRdROqnwChscBsbvBZb1zLy4e8kP1erE9mOPLoh6afcbOgcPGpeMuauYR8fulX9u5SonSwYN5ms02yd1Z07z9bPZVX9reGuDDJwigALp2gHCl6S8AEWU0sRsZJnadtRbyBOK81pw73OOqtb8JoRPk3415vx279PmJAVt6x3bm47F50yTebSjxAesEDvA9NYCkVWDJCQFppfpHjon2KeoDmdHLmEu9yEzA1dMDIdJCR_4Br8fEw5cqnly2_vUSuSbNvaxfD9OBWmD2X5CoD_6uobS6IW2EzmYJfVGVdOBUqW-QkgYIC2SDVszd1z2CSCix__jQxjllQPXBp0HL864KVErsdUytBL1J0LTJPJQOCBFaTZCGIpyw0EvPqSZ3S5dXi8SolRQm3YK_7SQVI3-4JTNuwzi3G-UeB_M4&blockedUrl=http%3A%2F%2Fad.turn.com%2Fserver%2Fads.js%3Fpub%3D5766351%26cch%3D5766918%26code%3D5766926%26l%3D728x90%26aid%3D35714892%26ahcid%3D11498647%26bimpd%3D%7E4loWx2hBbQDrGFCWDJjke1g42uW7RLTdWiesOx0b3B1KFjiJdGJoNfXmferT63OHUr9sDQYZFDbJH2q768FJZ5h8VG1agJSao30l5WNLKv98yrSWaIELfnH2EFg1BHn3Q8IoGll5sNh0d9FxmcEi9 --eSKj7A7w6cH9Uhu3Nm16AqSNNHfE4_5G_-9t-Ty8MARdvMjrN4H1TX56t3ZdWC9UlEQeIel1lknbmbr9Ki0BWJgEJ-IAqMeVM4yqSn7-2I7cC7e0MtWLbi-AQS39wHwwBttsHbCFeMUOo7uVI70hx392w1cRHX2Hf1Px-Q9fg9k0GTY4hk80E_fsRESHoIpJoCa5Sh7bV9Qa8kly74cRvIvtPhHNjp8xxta0c-ku_8_nm42UVG4C39s HTTP/1.1" 200 2479 -/- (-%) "http://ad.doubleclick.net/N3847/adi/ebay.uk.x.mmp;sz=728x90;ord=1434020649?" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)" "1.1 CWS_proxy" "195.212.10.34" 0'; 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