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 (24)

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

/
/
gmx

Test String

Code Generator

Generated Code

const regex = /(?#regexp & naming based on RFC5424) ^<(?<priority>\d|\d{2}|1[1-8]\d|19[01])>(?<version>\d{1,2})\s (?<timestamp>-|(?<fullyear>[12]\d{3})-(?<month>0\d|[1][012])-(?<mday>[012]\d|3[01])T(?<hour>[01]\d|2[0-4]):(?<minute>[0-5]\d):(?<second>[0-5]\d|60)(?#60seconds can be used for leap year!)(?:\.(?<secfrac>\d{1,6}))?(?<numoffset>Z|[+-]\d{2}:\d{2})(?#=timezone))\s (?<hostname>[\S]{1,255})\s (?<appname>[\S]{1,48})\s (?<procid>[\S]{1,128})\s (?<msgid>[\S]{1,32})\s (?<structureddata>-|(?:\[.+?(?<!\\)\])+) (?:\s(?<msg>.+))?$/gm; // Alternative syntax using RegExp constructor // const regex = new RegExp('(?#regexp & naming based on RFC5424) ^<(?<priority>\\d|\\d{2}|1[1-8]\\d|19[01])>(?<version>\\d{1,2})\\s (?<timestamp>-|(?<fullyear>[12]\\d{3})-(?<month>0\\d|[1][012])-(?<mday>[012]\\d|3[01])T(?<hour>[01]\\d|2[0-4]):(?<minute>[0-5]\\d):(?<second>[0-5]\\d|60)(?#60seconds can be used for leap year!)(?:\\.(?<secfrac>\\d{1,6}))?(?<numoffset>Z|[+-]\\d{2}:\\d{2})(?#=timezone))\\s (?<hostname>[\\S]{1,255})\\s (?<appname>[\\S]{1,48})\\s (?<procid>[\\S]{1,128})\\s (?<msgid>[\\S]{1,32})\\s (?<structureddata>-|(?:\\[.+?(?<!\\\\)\\])+) (?:\\s(?<msg>.+))?$', 'gm') const str = `<165>1 2003-08-24T05:14:15.000003-07:00 192.0.2.1 myproc 8710 - - %% It's time to make the do-nuts. <34>1 2003-10-11T22:14:15.003Z mymachine.example.com su - ID47 - BOM'su root' failed for lonvick on /dev/pts/8 <190>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="10\\] 11"] BOMAn application event log entry..[ ] sadasd <25>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"][examplePriority@32473 class="high"] <1>12 - mymachine - - ID47 - asd asdaasd`; // Reset `lastIndex` if this regex is defined globally // regex.lastIndex = 0; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }

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 JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions