Regular Expressions 101

Save & Share

  • Regex Version: ver. 4
  • 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

/
/
imsg

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 = "(?<=^|;|<\\?php\\s|<\\?\\s)(\\s*?)(\\h*define\\s*\\(\\s*[\\'\"](\\w*?)[\\'\"]\\s*)(,\\s*([\\'\"].*?[\\'\"]|.*?)\\s*)((?:,\\s*(?:true|false)\\s*)?\\)\\s*;)"; final String string = "<?php\n" + "/**\n" + " * The base configuration for WordPress\n" + " *\n" + " * The wp-config.php creation script uses this file during the\n" + " * installation. You don't have to use the web site, you can\n" + " * copy this file to \"wp-config.php\" and fill in the values.\n" + " *\n" + " * This file contains the following configurations:\n" + " *\n" + " * * MySQL settings\n" + " * * Secret keys\n" + " * * Database table prefix\n" + " * * ABSPATH\n" + " *\n" + " * @link https://codex.wordpress.org/Editing_wp-config.php\n" + " *\n" + " * @package WordPress\n" + " */\n\n" + "// ** MySQL settings - You can get this info from your web host ** //\n" + "/** The name of the database for WordPress */\n" + "define('DB_NAME', 'database_name_here');\n\n" + "/** MySQL database username */\n" + "define('DB_USER', 'username_here');\n\n" + "/** MySQL database password */\n" + "define('DB_PASSWORD', 'password_here');\n\n" + "/** MySQL hostname */\n" + "define('DB_HOST', 'localhost');\n\n" + "/** Database Charset to use in creating database tables. */\n" + "define('DB_CHARSET', 'utf8');\n\n" + "/** The Database Collate type. Don't change this if in doubt. */\n" + "define('DB_COLLATE', '');\n\n" + "/**#@+\n" + " * Authentication Unique Keys and Salts.\n" + " *\n" + " * Change these to different unique phrases!\n" + " * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}\n" + " * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.\n" + " *\n" + " * @since 2.6.0\n" + " */\n" + "define('AUTH_KEY', 'put your unique phrase here');\n" + "define('SECURE_AUTH_KEY', 'put your unique phrase here');\n" + "define('LOGGED_IN_KEY', 'put your unique phrase here');\n" + "define('NONCE_KEY', 'put your unique phrase here');\n" + "define('AUTH_SALT', 'put your unique phrase here');\n" + "define('SECURE_AUTH_SALT', 'put your unique phrase here');\n" + "define('LOGGED_IN_SALT', 'put your unique phrase here');\n" + "define('NONCE_SALT', 'put your unique phrase here');\n\n" + "/**#@-*/\n\n" + "/**\n" + " * WordPress Database Table prefix.\n" + " *\n" + " * You can have multiple installations in one database if you give each\n" + " * a unique prefix. Only numbers, letters, and underscores please!\n" + " */\n" + "$table_prefix = 'wp_';\n\n" + "/**\n" + " * For developers: WordPress debugging mode.\n" + " *\n" + " * Change this to true to enable the display of notices during development.\n" + " * It is strongly recommended that plugin and theme developers use WP_DEBUG\n" + " * in their development environments.\n" + " *\n" + " * For information on other constants that can be used for debugging,\n" + " * visit the Codex.\n" + " *\n" + " * @link https://codex.wordpress.org/Debugging_in_WordPress\n" + " */\n" + "define('WP_DEBUG', false);\n\n" + "//define( 'TEST_UPDATE_NO_MATCH', 'updateme' );\n" + "// define( 'TEST_UPDATE_NO_MATCH', 'updateme' );\n\n" + "define( 'TEST_MULTIPLE_LINE_1', 'update);me' );define( 'TEST_MULTIPLE_LINE_2', 'updateme' ); define( 'TEST_MULTIPLE_LINE_3', 'updateme' );\n\n" + "define( 'TEST_UPDATE_1', 'updateme' );\n" + "define( 'TEST_UPDATE_2' , 'updateme' );\n" + "define(\n" + " 'TEST_UPDATE_3',\n" + " 'updateme'\n" + ");\n" + "define(\n" + " 'TEST_UPDATE_4'\n" + " ,\n" + " 'updateme'\n" + " )\n" + ";\n" + " define (\n" + " 'TEST_UPDATE_5'\n" + ",\n" + " 'updateme'\n" + ")\n" + " ;\n" + "define( \"TEST_UPDATE_6\",'updateme' );\n" + "define ( \"TEST_UPDATE_7\",'updateme' );\n" + "define('TEST_UPDATE_8','updateme');\n" + "DEFINE( 'TEST_UPDATE_9', 'updateme' );\n" + "DEFINE ('TEST_UPDATE_10' ,'updateme' );\n" + "define( 'TEST_UPDATE_11', 'updateme', false );\n" + "define( 'TEST_UPDATE_12' , 'updateme' , false );\n" + "define(\n" + " 'TEST_UPDATE_13',\n" + " 'updateme',\n" + " false\n" + ");\n" + "define(\n" + " 'TEST_UPDATE_14'\n" + " ,\n" + " 'updateme',\n" + " false\n" + " )\n" + ";\n" + " define (\n" + " 'TEST_UPDATE_15'\n" + ",\n" + " 'updateme'\n" + " ,\n" + "false\n" + ")\n" + " ;\n" + "define( \"TEST_UPDATE_16\",'updateme' ,false );\n" + "define ( \"TEST_UPDATE_17\",'updateme',false );\n" + "define('TEST_UPDATE_18','updateme',false);\n" + "DEFINE('TEST_UPDATE_19', 'updateme', false);\n" + "DEFINE ('TEST_UPDATE_20' ,'updateme' ,false );\n\n" + "/* That's all, stop editing! Happy blogging. */\n\n" + "/** Absolute path to the WordPress directory. */\n" + "if ( !defined('ABSPATH') )\n" + " define('ABSPATH', dirname(__FILE__) . '/');\n\n" + "/** Sets up WordPress vars and included files. */\n" + "require_once(ABSPATH . 'wp-settings.php');\n"; final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL); 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