Regular Expressions 101

Save & Share

  • Regex Version: ver. 5
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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

Substitution

Processing...

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 = "\\$([^\\{(\"\\\\'\\/])([a-zA-Z0-9_]*)([^\\}\\/ \\t'\"\\n.\\]:(=\\\\-]*)"; final String string = "amavis_permissions_status=$(find -H $amavis_state_dir -maxdepth 3 -a \\( \\! -user amavis -o \\! -group amavis \\))\n\n" + " if [[ -n $amavis_permissions_status ]]\n" + " then\n" + " _notify 'inf' \"Fixing $amavis_state_dir permissions\"\n" + " chown -hR amavis:amavis $amavis_state_dir\n" + " else\n" + " _notify 'inf' \"Permissions in $amavis_state_dir look OK\"\n" + " return 0\n" + " fi\n" + "}\n\n" + "function _fix_cleanup_clamav()\n" + "{\n" + " _notify 'task' 'Cleaning up disabled Clamav'\n" + " rm -f /etc/logrotate.d/clamav-*\n" + " rm -f /etc/cron.d/clamav-freshclam\n" + "}\n\n" + "function _fix_cleanup_spamassassin()\n" + "{\n" + " _notify 'task' 'Cleaning up disabled spamassassin'\n" + " rm -f /etc/cron.daily/spamassassin\n" + "}\n\n" + "##########################################################################\n" + "# << Fix Stack\n" + "##########################################################################\n\n\n" + "##########################################################################\n" + "# >> Misc Stack\n" + "#\n" + "# Description: Place functions that do not fit in the sections above here\n" + "##########################################################################\n\n" + "function misc()\n" + "{\n" + " _notify 'taskgrp' 'Starting Misc'\n\n" + " for _func in \"${FUNCS_MISC[@]}\"\n" + " do\n" + " if ! $_func\n" + " then\n" + " _defunc\n" + " fi\n" + " done\n" + "}\n\n" + "function _misc_save_states()\n" + "{\n" + " # consolidate all states into a single directory (`/var/mail-state`) to allow persistence using docker volumes\n" + " statedir=/var/mail-state\n\n" + " if [[ $ONE_DIR -eq 1 ]] && [[ -d $statedir ]]\n" + " then\n" + " _notify 'inf' \"Consolidating all state onto $statedir\"\n\n" + " local FILES=(\n" + " /var/spool/postfix\n" + " /var/lib/postfix\n" + " /var/lib/amavis\n" + " /var/lib/clamav\n" + " /var/lib/spamassassin\n" + " /var/lib/fail2ban\n" + " /var/lib/postgrey\n" + " /var/lib/dovecot\n" + " )\n\n" + " for d in \"${FILES[@]}\"\n" + " do\n" + " dest=\"$statedir/$(echo \"$d\" | sed -e 's/.var.//; s/\\//-/g')\"\n\n" + " if [[ -d $dest ]]\n" + " then\n" + " _notify 'inf' \" Destination $dest exists, linking $d to it\"\n" + " rm -rf \"$d\"\n" + " ln -s \"$dest\" \"$d\"\n" + " elif [[ -d $d ]]\n" + " then\n" + " _notify 'inf' \" Moving contents of $d to $dest:\" \"$(ls \"$d\")\"\n" + " mv \"$d\" \"$dest\"\n" + " ln -s \"$dest\" \"$d\"\n" + " else\n" + " _notify 'inf' \" Linking $d to $dest\"\n" + " mkdir -p \"$dest\"\n" + " ln -s \"$dest\" \"$d\"\n" + " fi\n" + " done\n\n" + " _notify 'inf' 'Fixing /var/mail-state/* permissions'\n" + " chown -R clamav /var/mail-state/lib-clamav\n" + " chown -R postfix /var/mail-state/lib-postfix\n" + " chown -R postgrey /var/mail-state/lib-postgrey\n" + " chown -R debian-spamd /var/mail-state/lib-spamassassin\n" + " chown -R postfix /var/mail-state/spool-postfix\n\n" + " fi\n" + "}\n\n" + "##########################################################################\n" + "# >> Start Daemons\n" + "##########################################################################\n\n" + "function start_daemons()\n" + "{\n" + " _notify 'taskgrp' 'Starting mail server'\n\n" + " for _func in \"${DAEMONS_START[@]}\"\n" + " do\n" + " if ! $_func\n" + " then\n" + " _defunc\n" + " fi\n" + " done\n" + "}\n\n" + "function _start_daemons_cron()\n" + "{\n" + " _notify 'task' 'Starting cron' 'n'\n" + " supervisorctl start cron\n" + "}\n\n" + "function _start_daemons_rsyslog()\n" + "{\n" + " _notify 'task' 'Starting rsyslog ' 'n'\n" + " supervisorctl start rsyslog\n" + "}\n\n" + "function _start_daemons_saslauthd()\n" + "{\n" + " _notify 'task' 'Starting saslauthd' 'n'\n" + " supervisorctl start \"saslauthd_${SASLAUTHD_MECHANISMS}\"\n" + "}\n\n" + "function _start_daemons_fail2ban()\n" + "{\n" + " _notify 'task' 'Starting fail2ban ' 'n'\n" + " touch /var/log/auth.log\n\n" + " # delete fail2ban.sock that probably was left here after container restart\n" + " if [[ -e /var/run/fail2ban/fail2ban.sock ]]\n" + " then\n" + " rm /var/run/fail2ban/fail2ban.sock\n" + " fi\n\n" + " supervisorctl start fail2ban\n" + "}\n\n" + "function _start_daemons_opendkim()\n" + "{\n" + " _notify 'task' 'Starting opendkim ' 'n'\n" + " supervisorctl start opendkim\n" + "}\n\n" + "function _start_daemons_opendmarc()\n" + "{\n" + " _notify 'task' 'Starting opendmarc ' 'n'\n" + " supervisorctl start opendmarc\n" + "}\n\n" + "function _start_daemons_postsrsd()\n" + "{\n" + " _notify 'task' 'Starting postsrsd ' 'n'\n" + " supervisorctl start postsrsd\n" + "}\n\n" + "function _start_daemons_postfix()\n" + "{\n" + " _notify 'task' 'Starting postfix' 'n'\n" + " supervisorctl start postfix\n" + "}\n\n" + "function _start_daemons_dovecot()\n" + "{\n" + " # Here we are starting sasl and imap, not pop3 because it's disabled by default\n" + " _notify 'task' 'Starting dovecot services' 'n'\n\n" + " if [[ $ENABLE_POP3 -eq 1 ]]\n" + " then\n" + " _notify 'task' 'Starting pop3 services' 'n'\n" + " mv /etc/dovecot/protocols.d/pop3d.protocol.disab /etc/dovecot/protocols.d/pop3d.protocol\n" + " fi\n\n" + " if [[ -f /tmp/docker-mailserver/dovecot.cf ]]\n" + " then\n" + " cp /tmp/docker-mailserver/dovecot.cf /etc/dovecot/local.conf\n" + " fi\n\n" + " supervisorctl start dovecot\n\n" + " # TODO fix: on integration test\n" + " # doveadm: Error: userdb lookup: connect(/var/run/dovecot/auth-userdb) failed: No such file or directory\n" + " # doveadm: Fatal: user listing failed\n\n" + " # if [[ $ENABLE_LDAP -ne 1 ]]\n" + " # then\n" + " # echo \"Listing users\"\n" + " # /usr/sbin/dovecot user '*'\n" + " # fi\n" + "}\n\n" + "function _start_daemons_fetchmail()\n" + "{\n" + " _notify 'task' 'Starting fetchmail' 'n'\n" + " /usr/local/bin/setup-fetchmail\n" + " supervisorctl start fetchmail\n" + "}\n\n" + "function _start_daemons_clamav()\n" + "{\n" + " _notify 'task' 'Starting clamav' 'n'\n" + " supervisorctl start clamav\n" + "}\n\n" + "function _start_daemons_postgrey()\n" + "{\n" + " _notify 'task' 'Starting postgrey' 'n'\n" + " rm -f /var/run/postgrey/postgrey.pid\n" + " supervisorctl start postgrey\n" + "}\n\n" + "function _start_daemons_amavis()\n" + "{\n" + " _notify 'task' 'Starting amavis' 'n'\n" + " supervisorctl start amavis\n" + "}\n\n" + "##########################################################################\n" + "# << Start Daemons\n" + "##########################################################################\n\n\n" + "##########################################################################\n" + "# Start check for update postfix-accounts and postfix-virtual\n" + "##########################################################################\n\n" + "function _start_changedetector()\n" + "{\n" + " _notify 'task' 'Starting changedetector' 'n'\n" + " supervisorctl start changedetector\n" + "}\n\n" + "# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" + "# ! CARE --> DON'T CHANGE, unless you exactly know what you are doing\n" + "# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n" + "# shellcheck source=/dev/null\n" + ". /usr/local/bin/helper_functions.sh\n\n" + "if [[ ${DEFAULT_VARS[\"DMS_DEBUG\"]} -eq 1 ]]\n" + "then\n" + " _notify 'taskgrp' \"\"\n" + " _notify 'taskgrp' \"#\"\n" + " _notify 'taskgrp' \"#\"\n" + " _notify 'taskgrp' \"# ENV\"\n" + " _notify 'taskgrp' \"#\"\n" + " _notify 'taskgrp' \"#\"\n" + " _notify 'taskgrp' \"\"\n" + " printenv\n" + "fi\n\n" + "_notify 'taskgrp' \"\"\n" + "_notify 'taskgrp' \"#\"\n" + "_notify 'taskgrp' \"#\"\n" + "_notify 'taskgrp' \"# docker-mailserver\"\n" + "_notify 'taskgrp' \"#\"\n" + "_notify 'taskgrp' \"#\"\n" + "_notify 'taskgrp' \"\"\n\n" + "register_functions\n\n" + "check\n" + "setup\n" + "fix\n" + "misc\n" + "start_daemons\n\n" + "_notify 'taskgrp' \"\"\n" + "_notify 'taskgrp' \"#\"\n" + "_notify 'taskgrp' \"# $HOSTNAME is up and running\"\n" + "_notify 'taskgrp' \"#\"\n" + "_notify 'taskgrp' \"\"\n\n" + "touch /var/log/mail/mail.log\n" + "tail -fn 0 /var/log/mail/mail.log\n\n" + "# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" + "# ! CARE --> DON'T CHANGE, unless you exactly know what you are doing\n" + "# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n" + "exit 0\n\n"; final String subst = "${$1$2$3}"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); // The substituted value will be contained in the result variable final String result = matcher.replaceAll(subst); System.out.println("Substitution result: " + result); } }

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