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

/
/
gmx

Test String

Code Generator

Generated Code

const regex = /^(?<time>[^ ][-:+0-9TZ]+|[[:upper:]][[:lower:]]{2}[ -:+0-9TZ]*)\s+(?<messages>.*)/gm; // Alternative syntax using RegExp constructor // const regex = new RegExp('^(?<time>[^ ][-:+0-9TZ]+|[[:upper:]][[:lower:]]{2}[ -:+0-9TZ]*)\\s+(?<messages>.*)', 'gm') const str = `# suse 2017-09-19T10:13:33+00:00 de4-1d-2d-0d-44-a0 ceilometer-polling: AMQP server on 10.20.97.42:5672 is unreachable: [Errno 113] EHOSTUNREACH. Trying again in 2 seconds. 2017-09-19T10:13:47+00:00 d2c-76-8a-ac-64-6e ceilometer-expirer: Unable to reconnect to the primary mongodb after 10 retries. Giving up. 2017-09-19T10:13:47+00:00 d2c-76-8a-ac-64-6e ceilometer-expirer: ServerSelectionTimeoutError: 10.60.2.83:27017: [Errno 111] ECONNREFUSED,10.60.2.85:27017: [Errno 111] ECONNREFUSED,10.60.2.84:27017: [Errno 111] ECONNREFUSED 2017-07-28T12:26:02+00:00 de4-1d-2d-0d-44-a0 ceilometer-polling: Polling pollster cpu_util in the context of meter_source 2017-07-28T12:26:02+00:00 de4-1d-2d-0d-44-a0 ceilometer-polling: Polling pollster memory.usage in the context of meter_source 2017-07-28T12:26:02+00:00 de4-1d-2d-0d-44-a0 ceilometer-polling: Cannot inspect data of MemoryUsagePollster for 7f042103-7954-450e-8d8a-c7e48f8c29f4, non-fatal reason: Failed to inspect memory usage of instance <name=instance-0003cb17, id=7f042103-7954-450e-8d8a-c7e48f8c29f4>, can not get info from libvirt. 2017-07-28T12:26:02+00:00 de4-1d-2d-0d-44-a0 ceilometer-polling: Instance 213e62de-5b97-47c2-90f3-3afa794bafbb was shut off while getting samples of MemoryUsagePollster: Failed to inspect data of instance <name=instance-0003c936, id=213e62de-5b97-47c2-90f3-3afa794bafbb>, domain state is SHUTOFF. # ubuntu Sep 21 06:25:36 ctl01 haproxy[24057]: 172.16.10.102:41194 [21/Sep/2017:06:25:36.349] mysql_cluster mysql_cluster/ctl01 1/0/519 3680 -- 105/97/97/97/0 0/0 Sep 21 06:25:37 ctl01 haproxy[24057]: 172.16.10.102:41132 [21/Sep/2017:06:25:33.786] mysql_cluster mysql_cluster/ctl01 1/0/3571 8885 -- 104/96/96/96/0 0/0 Sep 21 06:25:37 ctl01 haproxy[24057]: Server keystone_admin_api/ctl01 is DOWN, reason: Layer7 timeout, check duration: 10001ms. 2 active and 0 backup servers left. 1 sessions active, 0 requeued, 0 remaining in queue. Sep 21 06:25:37 ctl01 haproxy[24057]: Server keystone_admin_api/ctl01 is DOWN, reason: Layer7 timeout, check duration: 10001ms. 2 active and 0 backup servers left. 1 sessions active, 0 requeued, 0 remaining in queue. Sep 21 06:25:38 ctl01 haproxy[24057]: 172.16.10.103:35860 [21/Sep/2017:06:25:35.639] mysql_cluster mysql_cluster/ctl01 1/0/2567 8885 -- 104/96/96/96/0 0/0 Sep 21 06:25:38 ctl01 haproxy[24057]: 172.16.10.102:41230 [21/Sep/2017:06:25:37.839] mysql_cluster mysql_cluster/ctl01 1/0/618 3680 -- 104/96/96/96/0 0/0 Sep 21 06:25:38 ctl01 haproxy[24057]: 172.16.10.103:35920 [21/Sep/2017:06:25:38.384] mysql_cluster mysql_cluster/ctl01 1/0/526 3680 -- 103/95/95/95/0 0/0 Sep 21 06:25:39 ctl01 haproxy[24057]: Server cinder_api/ctl01 is DOWN, reason: Layer7 timeout, check duration: 10001ms. 2 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. `; // 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