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

/
/
gm

Test String

Code Generator

Generated Code

const regex = /^t=(?<longtime>[^ ]+)\s*lvl=(?<loglevel>[^ ]+)\s*msg=(?<msg>(?:"[^"]+"|[^ ]+))\s*logger=(?<logger>[^ ]+)\s*(?:addresses=(?<email>(?:"\[[^"]+\]"|[^ ]+)))?(?:userId=(?<userID>[^ ]+))?\s*(?:orgId=(?<orgID>[^ ]+))?\s*(?:uname=(?<uname>[^ ]+|[??]*))?\s*(?:method=(?<method>[^ ]+))?\s*(?:path=(?<path>(?:"[^"]+"|[^ ]+)))?\s*(?:ruleId=(?<ruleID>[^ ]+))?\s*(?:name=(?<name>(?:"[^"]+"|[^ ]+)))?\s*(?:error=(?<error>(?:"[^"]+"|[^ ]+)))?\s*(?:changing state to=(?<ChStateTo>[^ ]+))?\s*(?:status=(?<status>[^ ]+))?\s*(?:remote_addr=(?<remote_addr>[^ ]+))?\s*(?:time_ms=(?<time_ms>[^ ]+))?\s*(?:size=(?<size>[^ ]+))?\s*(?:referer=(?<referer>(?:"[^"]+"|[^ ]+)|[??]*))?\s*(?:alertId=(?<alertID>[^ ]+))?\s*(?:newState=(?<NewState>[^ ]+))?\s*(?:prev state=(?<prevState>[^ ]+))?\s*(?:url=(?<url>[^ ]+))?$/gm; // Alternative syntax using RegExp constructor // const regex = new RegExp('^t=(?<longtime>[^ ]+)\\s*lvl=(?<loglevel>[^ ]+)\\s*msg=(?<msg>(?:"[^"]+"|[^ ]+))\\s*logger=(?<logger>[^ ]+)\\s*(?:addresses=(?<email>(?:"\\[[^"]+\\]"|[^ ]+)))?(?:userId=(?<userID>[^ ]+))?\\s*(?:orgId=(?<orgID>[^ ]+))?\\s*(?:uname=(?<uname>[^ ]+|[??]*))?\\s*(?:method=(?<method>[^ ]+))?\\s*(?:path=(?<path>(?:"[^"]+"|[^ ]+)))?\\s*(?:ruleId=(?<ruleID>[^ ]+))?\\s*(?:name=(?<name>(?:"[^"]+"|[^ ]+)))?\\s*(?:error=(?<error>(?:"[^"]+"|[^ ]+)))?\\s*(?:changing state to=(?<ChStateTo>[^ ]+))?\\s*(?:status=(?<status>[^ ]+))?\\s*(?:remote_addr=(?<remote_addr>[^ ]+))?\\s*(?:time_ms=(?<time_ms>[^ ]+))?\\s*(?:size=(?<size>[^ ]+))?\\s*(?:referer=(?<referer>(?:"[^"]+"|[^ ]+)|[??]*))?\\s*(?:alertId=(?<alertID>[^ ]+))?\\s*(?:newState=(?<NewState>[^ ]+))?\\s*(?:prev state=(?<prevState>[^ ]+))?\\s*(?:url=(?<url>[^ ]+))?$', 'gm') const str = `t=2019-07-30T07:22:35+0300 lvl=eror msg="Alert Rule Result Error" logger=alerting.evalContext ruleId=22 name="Выдача сломалась!" error="Alert execution exceeded the timeout" changing state to=alerting t=2019-07-30T07:22:39+0300 lvl=info msg="New state change" logger=alerting.resultHandler alertId=48 newState=alerting prev state=ok t=2019-07-30T07:22:39+0300 lvl=info msg="uploaded screenshot of alert to external image store" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/TylgxQWG4Aiq6kmlSrM4.png t=2019-07-30T07:22:39+0300 lvl=info msg="uploaded screenshot of alert to external image store" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/lsfc738fTMkR034AZM2j.png t=2019-07-30T07:22:39+0300 lvl=info msg="Sending webhook" logger=alerting.notifier.webhook t=2019-07-30T07:22:39+0300 lvl=info msg="Sending webhook" logger=alerting.notifier.webhook t=2019-07-30T07:22:40+0300 lvl=eror msg="Alert Rule Result Error" logger=alerting.evalContext ruleId=89 name=Вход.Формы.Статусы error="Alert execution exceeded the timeout" changing state to=alerting t=2019-07-30T07:22:40+0300 lvl=info msg="New state change" logger=alerting.resultHandler alertId=89 newState=alerting prev state=ok t=2019-07-30T07:22:40+0300 lvl=eror msg="Alert Rule Result Error" logger=alerting.evalContext ruleId=88 name="Exception State alert" error="Alert execution exceeded the timeout" changing state to=alerting t=2019-07-30T07:22:40+0300 lvl=info msg="uploaded screenshot of alert to external image store" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/tQeLMXFNBU6y6USKOe5E.png t=2019-07-30T07:22:40+0300 lvl=info msg="Sending webhook" logger=alerting.notifier.webhook t=2019-07-30T07:22:42+0300 lvl=eror msg="Alert Rule Result Error" logger=alerting.evalContext ruleId=21 name="HermesApiErrors alert" error="Alert execution exceeded the timeout" changing state to=alerting t=2019-07-30T07:22:42+0300 lvl=eror msg="Alert Rule Result Error" logger=alerting.evalContext ruleId=26 name="Генерация отчетов на Jasper alert" error="Alert execution exceeded the timeout" changing state to=keep_state t=2019-07-30T07:22:45+0300 lvl=eror msg="Alert Rule Result Error" logger=alerting.evalContext ruleId=32 name="СРВО. Фоновые задачи" error="Alert execution exceeded the timeout" changing state to=alerting t=2019-07-30T07:22:45+0300 lvl=info msg="New state change" logger=alerting.resultHandler alertId=32 newState=alerting prev state=ok t=2019-07-30T07:22:45+0300 lvl=info msg="uploaded screenshot of alert to external image store" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/yZzqi4QmGCvSbYraq3UC.png t=2019-07-30T07:22:45+0300 lvl=info msg="Sending webhook" logger=alerting.notifier.webhook t=2019-07-30T07:22:45+0300 lvl=info msg="Sending alert notification to" logger=alerting.notifier.email addresses="[Andrey.Kudashkin@russianpost.ru Arkhipov.Alexander@russianpost.ru szhogin@luxoft.com OPodus@luxoft.com]" t=2019-07-30T07:22:47+0300 lvl=eror msg="Alert Rule Result Error" logger=alerting.evalContext ruleId=135 name="pochtaid-sso alert" error="Alert execution exceeded the timeout" changing state to=alerting t=2019-07-30T07:22:47+0300 lvl=info msg="New state change" logger=alerting.resultHandler alertId=135 newState=alerting prev state=ok t=2019-07-30T07:22:47+0300 lvl=eror msg="Alert Rule Result Error" logger=alerting.evalContext ruleId=90 name="Профиль. Статус" error="Alert execution exceeded the timeout" changing state to=alerting t=2019-07-30T07:22:47+0300 lvl=info msg="New state change" logger=alerting.resultHandler alertId=90 newState=alerting prev state=ok t=2019-07-30T07:22:47+0300 lvl=eror msg="Alert Rule Result Error" logger=alerting.evalContext ruleId=136 name="Normalization alert" error="Alert execution exceeded the timeout" changing state to=alerting t=2019-07-30T07:22:47+0300 lvl=info msg="New state change" logger=alerting.resultHandler alertId=136 newState=alerting prev state=ok t=2019-07-30T07:22:47+0300 lvl=info msg="uploaded screenshot of alert to external image store" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/qXR85haxAmRX5g1vqrmT.png t=2019-07-30T07:22:47+0300 lvl=info msg="uploaded screenshot of alert to external image store" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/dOfYePDh7n3oreQhaRCR.png t=2019-07-30T07:22:47+0300 lvl=info msg="Sending webhook" logger=alerting.notifier.webhook t=2019-07-30T07:22:47+0300 lvl=info msg="uploaded screenshot of alert to external image store" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/4eIoX3qCRuF16DoXNbRU.png `; // 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