Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
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
Processing...

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+(?:re)?start.*?(haproxy_check).*?;;"; final String string = "case \"$1\" in\n" + " start)\n" + " echo -n \"Starting haproxy \"\n" + " ## Start daemon with startproc(8). If this fails\n" + " ## the return value is set appropriately by startproc.\n" + " haproxy_check\n" + " /sbin/startproc $HAPROXY_BIN -D -f $HAPROXY_CONF -p $HAPROXY_PID\n" + " # Remember status and be verbose\n" + " rc_status -v\n" + " ;;\n" + " stop)\n" + " echo -n \"Shutting down haproxy \"\n" + " ## Stop daemon with killproc(8) and if this fails\n" + " ## killproc sets the return value according to LSB.\n\n" + " /sbin/killproc -TERM $HAPROXY_BIN\n\n" + " # this is additional forcing kill command to ensure that all processes are stopped.\n" + " /usr/bin/killall -9 $HAPROXY_BIN || true\n" + " # Remember status and be verbose\n" + " rc_status -v\n" + " ;;\n" + " try-restart|condrestart)\n" + " ## Do a restart only if the service was active before.\n" + " ## Note: try-restart is now part of LSB (as of 1.9).\n" + " ## RH has a similar command named condrestart.\n" + " if test \"$1\" = \"condrestart\"; then\n" + " echo \"${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}\"\n" + " fi\n" + " $0 status\n" + " if test $? = 0; then\n" + " # we us reload here for a graceful restart during update\n" + " $0 reload\n" + " else\n" + " rc_reset # Not running is not a failure.\n" + " fi\n" + " # Remember status and be quiet\n" + " rc_status\n" + " ;;\n" + " restart)\n" + " ## Stop the service and regardless of whether it was\n" + " ## running or not, start it again.\n" + " $0 stop\n" + " $0 start\n" + " haproxy_check\n" + " # Remember status and be quiet\n" + " rc_status\n" + " ;;\n" + " check)\n" + " ## Stop the service and regardless of whether it was\n" + " ## running or not, start it again.\n" + " echo -n \"Checking config of haproxy \"\n" + " haproxy_check\n" + " rc_status -v\n" + " ;;\n" + " reload|force-reload)\n" + " ## Like force-reload, but if daemon does not support\n" + " ## signaling, do nothing (!)\n\n" + " haproxy_check\n" + " # If it supports signaling:\n" + " echo -n \"Reload service haproxy \"\n" + " $HAPROXY_BIN -p $HAPROXY_PID -D -f $HAPROXY_CONF -sf $(cat $HAPROXY_PID)\n" + " rc_status -v\n" + " ;;\n" + " status)\n" + " echo -n \"Checking for service haproxy \"\n" + " ## Check status with checkproc(8), if process is running\n" + " ## checkproc will return with exit status 0.\n\n" + " # Return value is slightly different for the status command:\n" + " # 0 - service up and running\n" + " # 1 - service dead, but /var/run/ pid file exists\n" + " # 2 - service dead, but /var/lock/ lock file exists\n" + " # 3 - service not running (unused)\n" + " # 4 - service status unknown :-(\n" + " # 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)\n\n" + " # NOTE: checkproc returns LSB compliant status values.\n" + " /sbin/checkproc $HAPROXY_BIN\n" + " # NOTE: rc_status knows that we called this init script with\n" + " # \"status\" option and adapts its messages accordingly.\n" + " rc_status -v\n" + " ;;\n" + " probe)\n" + " ## Optional: Probe for the necessity of a reload, print out the\n" + " ## argument to this init script which is required for a reload.\n" + " ## Note: probe is not (yet) part of LSB (as of 1.9)\n\n" + " test $HAPROXY_CONF -nt $HAPROXY_PID && echo reload\n" + " ;;\n" + " *)\n" + " echo \"Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}\"\n" + " exit 1\n" + " ;;\n" + "esac"; final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | 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