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

/
/
gxm

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 = "^(?<time>[^ +][-:0-9TZ]+|[[:upper:]][[:lower:]]{2}[ -:+0-9TZ]*)([^ ][+0:]+|\\s+)(\\s*)(?<host>[^ ][-\\w\\d]+)(\\s*)(?<service>[^ [][-_\\w]+)(\\[(?<pid>[^ \\]][\\d]*)\\]):\\s*((\\[\\s*(?<mtime>[^ ][\\w: \\/]+)\\]\\s*\\[\\s*(?<severity>[^ ][\\w]+)\\]\\s*){0,1}\\s*\\[\\s*(?<component>[^ ][\\w:]+)\\]){0,1}\\s*(?<message>.*){0,1}.*$"; final String string = "Oct 02 12:56:47 ctl01 td-agent-bit[22911]: [3915] haproxy: [1503580119.000000000, {\"path\"=>\"/var/log/sapmock/haproxy.log\", \"time\"=>\"2017-08-24T13:08:39\", \"host\"=>\"de4-1d-2d-0d-44-a0\", \"service\"=>\"ceilometer-polling\", \"message\"=>\"AMQP serve\n" + "Oct 02 12:56:47 ctl01 td-agent-bit[22911]: [3916] haproxy: [1503580148.000000000, {\"path\"=>\"/var/log/sapmock/haproxy.log\", \"time\"=>\"2017-08-24T13:09:08\", \"host\"=>\"de4-1d-2d-0d-44-a0\", \"service\"=>\"ceilometer-polling\", \"message\"=>\"message re\n" + "Oct 02 12:56:47 ctl01 td-agent-bit[22911]: [3917] haproxy: [1503580151.000000000, {\"path\"=>\"/var/log/sapmock/haproxy.log\", \"time\"=>\"2017-08-24T13:09:11\", \"host\"=>\"de4-1d-2d-0d-44-a0\", \"service\"=>\"ceilometer-polling\", \"message\"=>\"AMQP serve\n" + "Oct 02 12:56:47 ctl01 td-agent-bit[22911]: [3918] haproxy: [1503580164.000000000, {\"path\"=>\"/var/log/sapmock/haproxy.log\", \"time\"=>\"2017-08-24T13:09:24\", \"host\"=>\"de4-1d-2d-0d-44-a0\", \"service\"=>\"ceilometer-polling\", \"message\"=>\"message re\n" + "Oct 02 12:56:47 ctl01 td-agent-bit[22911]: [3919] haproxy: [1503580167.000000000, {\"path\"=>\"/var/log/sapmock/haproxy.log\", \"time\"=>\"2017-08-24T13:09:27\", \"host\"=>\"de4-1d-2d-0d-44-a0\", \"service\"=>\"ceilometer-polling\", \"message\"=>\"AMQP serve\n" + "Oct 02 12:56:47 ctl01 td-agent-bit[22911]: [3920] haproxy: [1503580172.000000000, {\"path\"=>\"/var/log/sapmock/haproxy.log\", \"time\"=>\"2017-08-24T13:09:32\", \"host\"=>\"de4-1d-2d-0d-44-a0\", \"service\"=>\"ceilometer-polling\", \"message\"=>\"AMQP serve\n" + "Oct 02 12:58:52 ctl01 td-agent-bit[22911]: [engine] caught signal\n" + "Oct 02 12:58:52 ctl01 td-agent-bit[22911]: [2017/10/02 12:58:52] [ info] [input] pausing tail.0\n" + "Oct 02 12:58:52 ctl01 td-agent-bit[22911]: [2017/10/02 12:58:52] [ info] [input] pausing tail.1\n" + "Oct 02 12:58:52 ctl01 systemd[1]: Stopping TD Agent Bit...\n" + "Oct 02 13:00:22 ctl01 systemd[1]: td-agent-bit.service: State 'stop-sigterm' timed out. Killing.\n" + "Oct 02 13:00:22 ctl01 systemd[1]: td-agent-bit.service: Main process exited, code=killed, status=9/KILL\n" + "Oct 02 13:00:22 ctl01 systemd[1]: Stopped TD Agent Bit.\n" + "Oct 02 13:00:22 ctl01 systemd[1]: td-agent-bit.service: Unit entered failed state.\n" + "Oct 02 13:00:22 ctl01 systemd[1]: td-agent-bit.service: Failed with result 'signal'.\n" + "Oct 02 13:00:22 ctl01 systemd[1]: Started TD Agent Bit.\n" + "Oct 02 13:00:22 ctl01 td-agent-bit[23506]: [2017/10/02 13:00:22] [ info] [engine] started\n" + "Oct 02 13:00:22 ctl01 td-agent-bit[23506]: [2017/10/02 13:00:22] [error] [in_tail] parser 'rabbitmq' is not registered\n" + "Oct 02 13:00:22 ctl01 td-agent-bit[23506]: [2017/10/02 13:00:22] [ warn] [out_es] consider use a tag_key that starts with '_'\n" + "Oct 02 13:00:22 ctl01 td-agent-bit[23506]: [2017/10/02 13:00:22] [error] [parser:haproxy] Invalid time format %Y-%m-%dT%H:%M:%S.\n" + "Oct 02 13:00:22 ctl01 td-agent-bit[23506]: [2017/10/02 13:00:22] [error] [parser:haproxy] Invalid time format %Y-%m-%dT%H:%M:%S.\n" + "Oct 02 13:00:22 ctl01 td-agent-bit[23506]: [2017/10/02 13:00:22] [error] [parser:haproxy] Invalid time format %Y-%m-%dT%H:%M:%S.\n" + "Oct 02 13:00:22 ctl01 td-agent-bit[23506]: [2017/10/02 13:00:22] [error] [parser:haproxy] Invalid time format %Y-%m-%dT%H:%M:%S.\n" + "Oct 02 13:00:22 ctl01 td-agent-bit[23506]: [2017/10/02 13:00:22] [error] [parser:haproxy] Invalid time format %Y-%m-%dT%H:%M:%S.\n" + "Oct 02 13:00:22 ctl01 td-agent-bit[23506]: [2017/10/02 13:00:22] [error] [parser:haproxy] Invalid time format %Y-%m-%dT%H:%M:%S.\n"; final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS | 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