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 = /(?s)(?:.*(?:datetime:)(?<datetime>.*?)(?:$|\s*\|))?(?:.*(?:hostname:)(?<hostname>.*?)(?:$|\s*\|))?(?:.*(?:threadId:)(?<threadId>.*?)(?:$|\s*\|))?(?:.*(?:userId:)(?<userId>.*?)(?:$|\s*\|))?(?:.*(?:correlationId:)(?<correlationId>.*?)(?:$|\s*\|))?(?:.*(?:applicationName:)(?<applicationName>.*?)(?:$|\s*\|))?(?:.*(?:direction:)(?<direction>.*?)(?:$|\s*\|))?(?:.*(?:operationName:)(?<operationName>.*?)(?:$|\s*\|))?(?:.*(?:className:)(?<className>.*?)(?:$|\s*\|))?(?:.*(?:methodName:)(?<methodName>.*?)(?:$|\s*\|))?(?:.*(?:latency:)(?<latency>.*?)(?:$|\s*\|))?(?:.*(?:clusterName:)(?<clusterName>.*?)(?:$|\s*\|))?(?:.*(?:webServiceType:)(?<webServiceType>.*?)(?:$|\s*\|))?(?:.*(?:strCode:)(?<strCode>.*?)(?:$|\s*\|))?(?:.*(?:strDescription:)(?<strDescription>.*?)(?:$|\s*\|))?(?:.*(?:payload:)(?<payload>.*?)(?:$|\s*\|))?(?:.*(?:requestVerb:)(?<requestVerb>.*?)(?:$|\s*\|))?(?:.*(?:httpStatusCode:)(?<httpStatusCode>.*?)(?:$|\s*\|))?(?:.*(?:httpHeaders:)(?<httpHeaders>.*?)(?:$|\s*\|))?(?:.*(?:requestUrl:)(?<requestUrl>.*?)(?:$|\s*\|))?(?:.*(?:code:)(?<code>.*?)(?:$|\s*\|))?(?:.*(?:strSystemName:)(?<strSystemName>.*?)(?:$|\s*\|))?(?:.*(?:description:)(?<description>.*?)(?:$|\s*\|))?(?:.*(?:content\-length:)(?<content_length>.*?)(?:\s*\|))?(?:.*(?:logMessage:)(?<logMessage>.*?)(?:$|\s*\|))? /gm; // Alternative syntax using RegExp constructor // const regex = new RegExp('(?s)(?:.*(?:datetime:)(?<datetime>.*?)(?:$|\\s*\\|))?(?:.*(?:hostname:)(?<hostname>.*?)(?:$|\\s*\\|))?(?:.*(?:threadId:)(?<threadId>.*?)(?:$|\\s*\\|))?(?:.*(?:userId:)(?<userId>.*?)(?:$|\\s*\\|))?(?:.*(?:correlationId:)(?<correlationId>.*?)(?:$|\\s*\\|))?(?:.*(?:applicationName:)(?<applicationName>.*?)(?:$|\\s*\\|))?(?:.*(?:direction:)(?<direction>.*?)(?:$|\\s*\\|))?(?:.*(?:operationName:)(?<operationName>.*?)(?:$|\\s*\\|))?(?:.*(?:className:)(?<className>.*?)(?:$|\\s*\\|))?(?:.*(?:methodName:)(?<methodName>.*?)(?:$|\\s*\\|))?(?:.*(?:latency:)(?<latency>.*?)(?:$|\\s*\\|))?(?:.*(?:clusterName:)(?<clusterName>.*?)(?:$|\\s*\\|))?(?:.*(?:webServiceType:)(?<webServiceType>.*?)(?:$|\\s*\\|))?(?:.*(?:strCode:)(?<strCode>.*?)(?:$|\\s*\\|))?(?:.*(?:strDescription:)(?<strDescription>.*?)(?:$|\\s*\\|))?(?:.*(?:payload:)(?<payload>.*?)(?:$|\\s*\\|))?(?:.*(?:requestVerb:)(?<requestVerb>.*?)(?:$|\\s*\\|))?(?:.*(?:httpStatusCode:)(?<httpStatusCode>.*?)(?:$|\\s*\\|))?(?:.*(?:httpHeaders:)(?<httpHeaders>.*?)(?:$|\\s*\\|))?(?:.*(?:requestUrl:)(?<requestUrl>.*?)(?:$|\\s*\\|))?(?:.*(?:code:)(?<code>.*?)(?:$|\\s*\\|))?(?:.*(?:strSystemName:)(?<strSystemName>.*?)(?:$|\\s*\\|))?(?:.*(?:description:)(?<description>.*?)(?:$|\\s*\\|))?(?:.*(?:content\\-length:)(?<content_length>.*?)(?:\\s*\\|))?(?:.*(?:logMessage:)(?<logMessage>.*?)(?:$|\\s*\\|))? ', 'gm') const str = `[INFO] 28 Jun 2018 14:02:29,480 SYSLOG->|datetime:20180628140229479+0000|hostname:marketingprofile-deployment-877c7f857-cv9d6/10.44.0.15(10.44.0.15)|threadId:47-http-nio-11551-exec-1(main)|userId:null|correlationId:c7943b58-a584-4cba-ad44-45cf1af3a400|applicationName:RETRIEVEMARKETINGPROFILE-V1|direction:ENTERING|operationName:RetrievalService|className:LogServletFilter|methodName:logRequest|clusterName:TXH|webServiceType:REST|payload:<?xml version="1.0" encoding="UTF-8" standalone="no"?><!--<INPUTINFO> <BBCAN>8881008923</BBCAN> </INPUTINFO>--><INPUTINFO> <APPLICATIONID>NCOG</APPLICATIONID> <CLINETREQUESTID>test</CLINETREQUESTID> <VISION> <CUSTOMERID>151862438</CUSTOMERID> <ACCOUNTID>0001</ACCOUNTID> </VISION> <!--<VOICECAN>0178430862672</VOICECAN>--> <!-- <BBCAN>060002792607</BBCAN> --> <!--<BTN>2016221232</BTN> --> <!-- <ADDRESS> <ADDRESSID>030862772</ADDRESSID> <STATE>MD</STATE> </ADDRESS> --> <!--<SSPBAN>0178430862772</SSPBAN>--> <!--<PCAN>E100007652</PCAN> --> </INPUTINFO>|requestVerb:POST|httpHeaders:content-length={534};ssp-correlationid={c7943b58-a584-4cba-ad44-45cf1af3a400};host={10.44.0.15:11551};content-type={text/xml};connection={Keep-Alive};accept-encoding={gzip};user-agent={Apache-HttpClient/4.1.1 (java 1.5)};|requestUrl:http://10.44.0.15:11551/retrieveMarketingProfile/RetrievalService?null Security framework of XStream not initialized, XStream is probably vulnerable. [INFO] 28 Jun 2018 14:02:29,592 SYSLOG->|datetime:20180628140229591+0000|hostname:marketingprofile-deployment-877c7f857-cv9d6/10.44.0.15(10.44.0.15)|threadId:47-http-nio-11551-exec-1(main)|userId:null|correlationId:c7943b58-a584-4cba-ad44-45cf1af3a400|applicationName:RETRIEVEMARKETINGPROFILE-V1|direction:ENTERING|className:com.verizon.ssp.gateway.adapter.WebServiceAdaptor|clusterName:TXH|webServiceType:REST|payload:<!--<INPUTINFO> <BBCAN>8881008923</BBCAN> </INPUTINFO>--> <INPUTINFO> <APPLICATIONID>NCOG</APPLICATIONID> <CLINETREQUESTID>test</CLINETREQUESTID> <VISION> <CUSTOMERID>151862438</CUSTOMERID> <ACCOUNTID>0001</ACCOUNTID> </VISION> <!--<VOICECAN>0178430862672</VOICECAN>--> <!-- <BBCAN>060002792607</BBCAN> --> <!--<BTN>2016221232</BTN> --> <!-- <ADDRESS> <ADDRESSID>030862772</ADDRESSID> <STATE>MD</STATE> </ADDRESS> --> <!--<SSPBAN>0178430862772</SSPBAN>--> <!--<PCAN>E100007652</PCAN> --> </INPUTINFO>|content-length:534|logMessage:<!--<INPUTINFO> <BBCAN>8881008923</BBCAN> </INPUTINFO>--> <INPUTINFO> <APPLICATIONID>NCOG</APPLICATIONID> <CLINETREQUESTID>test</CLINETREQUESTID> <VISION> <CUSTOMERID>151862438</CUSTOMERID> <ACCOUNTID>0001</ACCOUNTID> </VISION> <!--<VOICECAN>0178430862672</VOICECAN>--> <!-- <BBCAN>060002792607</BBCAN> --> <!--<BTN>2016221232</BTN> --> <!-- <ADDRESS> <ADDRESSID>030862772</ADDRESSID> <STATE>MD</STATE> </ADDRESS> --> <!--<SSPBAN>0178430862772</SSPBAN>--> <!--<PCAN>E100007652</PCAN> --> </INPUTINFO> [ERROR] 28 Jun 2018 14:02:29,687 BaseDao : Consistency Level : ONE for Key Space: grid_mktprofile [INFO]threadId:47-http-nio-11551-exec-1(main)|userId:null|correlationId:c7943b58-a584-4cba-ad44-45cf1af3a400| [INFO]threadId:47-http-nio-11551-exec-1(main)|userId:null|correlationId:c7943b58-a584-4cba-ad44-45cf1af3a400| `; // 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