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

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String 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*\\|))?\n"; final String string = "[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\n" + "Security framework of XStream not initialized, XStream is probably vulnerable.\n" + "[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>\n" + "<BBCAN>8881008923</BBCAN>\n" + "</INPUTINFO>-->\n" + "<INPUTINFO>\n" + " <APPLICATIONID>NCOG</APPLICATIONID>\n" + " <CLINETREQUESTID>test</CLINETREQUESTID>\n" + "<VISION>\n" + " <CUSTOMERID>151862438</CUSTOMERID>\n" + " <ACCOUNTID>0001</ACCOUNTID>\n" + "</VISION>\n" + "<!--<VOICECAN>0178430862672</VOICECAN>-->\n" + "<!-- <BBCAN>060002792607</BBCAN> -->\n" + "<!--<BTN>2016221232</BTN> -->\n" + "<!-- <ADDRESS> \n" + " <ADDRESSID>030862772</ADDRESSID>\n" + " <STATE>MD</STATE>\n" + "</ADDRESS> -->\n" + "<!--<SSPBAN>0178430862772</SSPBAN>-->\n" + "<!--<PCAN>E100007652</PCAN> -->\n" + "</INPUTINFO>|content-length:534|logMessage:<!--<INPUTINFO>\n" + "<BBCAN>8881008923</BBCAN>\n" + "</INPUTINFO>-->\n" + "<INPUTINFO>\n" + " <APPLICATIONID>NCOG</APPLICATIONID>\n" + " <CLINETREQUESTID>test</CLINETREQUESTID>\n" + "<VISION>\n" + " <CUSTOMERID>151862438</CUSTOMERID>\n" + " <ACCOUNTID>0001</ACCOUNTID>\n" + "</VISION>\n" + "<!--<VOICECAN>0178430862672</VOICECAN>-->\n" + "<!-- <BBCAN>060002792607</BBCAN> -->\n" + "<!--<BTN>2016221232</BTN> -->\n" + "<!-- <ADDRESS> \n" + " <ADDRESSID>030862772</ADDRESSID>\n" + " <STATE>MD</STATE>\n" + "</ADDRESS> -->\n" + "<!--<SSPBAN>0178430862772</SSPBAN>-->\n" + "<!--<PCAN>E100007652</PCAN> -->\n" + "</INPUTINFO>\n" + "[ERROR] 28 Jun 2018 14:02:29,687 BaseDao : Consistency Level : ONE for Key Space: grid_mktprofile\n" + "[INFO]threadId:47-http-nio-11551-exec-1(main)|userId:null|correlationId:c7943b58-a584-4cba-ad44-45cf1af3a400|\n" + "[INFO]threadId:47-http-nio-11551-exec-1(main)|userId:null|correlationId:c7943b58-a584-4cba-ad44-45cf1af3a400|\n"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html