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
No Match

r"
"

Test String

Substitution

Processing...

Code Generator

Generated Code

package main import ( "regexp" "fmt" ) func main() { var re = regexp.MustCompile(`(?P<date>\S{3}\s\d{1,3}\s\d{2}\:\d{2}\:\d{2})\s(?P<device>\S+)\s\S\=(?P<src>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\:\s\S\=\"(?P<user>.*?)\"\s\S\=\d+\s\S\=\S\s\S\=\d+\s\S\=\d+\s\S+\=\d\s\S{1,3}\=.*?type=\".*?\"\s.*?meth=(?P<type>\S+)\s\S+\=\"(?P<url>.*?)\"\s\S{2}\=\"(?P<agent>.*?)\"\s\S{1,3}\=\"(?P<url2>.*?)dom\=\"(?P<domain>.*?)\".*?target_ip\=\"(?P<dest>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\".*`) var str = []byte(`Oct 18 06:27:03 WS2000.wnins.com h=172.16.20.74: u="WNINS\\rehetzel" s=200 X=+ t=1445149621 T=44767 Ts=0 act=1 cat="0x2200000008" app="-" rsn=- threat="-" type="image/jpeg" ctype="image/jpeg" sav-ev=- sav-dv=- uri-dv=- cache=- in=471 out=990 meth=GET ref="http://www.techrepublic.com/blog/the-enterprise-cloud/how-do-i-assign-permissions-to-users-to-see-sql-agent-jobs/" ua="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36" req="GET http://d.pzkysq.pink/5ET3nslK.jpg?z=715211411411 HTTP/1.1" dom="pzkysq.pink" filetype="-" rule="0" filesize=518 axtime=0.000455 fttime=0.000043 scantime=- src_cat="0x2000000008" labs_cat="0x2000000008" dcat_prox="-" target_ip="204.93.43.48" labs_rule_id="0" reqtime=0.000 adtime=0.000000 ftbypass=- os=Windows authn=17 auth_by=ntlm dnstime=0.000009 quotatime=-`) var substitution = []byte("") var count = 1 // negative counter is equivalent to global case (replace all) str = re.ReplaceAllStringFunc(str, func(s string) string { if count == 0 { return s } count -= 1 return re.ReplaceAllString(s, substitution) }) fmt.Println(string(str)) }

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 Golang, please visit: https://golang.org/pkg/regexp/