Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • 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
No Match

/
/

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"_gpfErrorDeclare\(\"([a-zA-Z\\]+)\", {\n((?:.*\n)*)\s*}\)" test_str = ("/**\n" " * @file Error base class\n" " * @since 0.1.5\n" " */\n" "/*#ifndef(UMD)*/\n" "\"use strict\";\n" "/*global _gpfExtend*/ // gpf.extend\n" "/*global _gpfIgnore*/ // Helper to remove unused parameter warning\n" "/*global _gpfObjectForEach*/ // Similar to [].forEach but for objects\n" "/*global _gpfStringCapitalize*/ // Capitalize the string\n" "/*global _gpfStringReplaceEx*/ // String replacement using dictionary map\n" "/*exported _gpfErrorDeclare*/ // Declare new gpf.Error names\n" "/*#endif*/\n\n" "/**\n" " * GPF Error class\n" " *\n" " * @constructor\n" " * @alias gpf.Error\n" " * @since 0.1.5\n" " */\n" "var _GpfError = gpf.Error = function () {};\n\n" "_GpfError.prototype = new Error();\n" "_gpfExtend(_GpfError.prototype, /** @lends gpf.Error.prototype */ {\n\n" " constructor: _GpfError,\n\n" " /**\n" " * Error code\n" " *\n" " * @readonly\n" " * @since 0.1.5\n" " */\n" " code: 0,\n\n" " /**\n" " * Error name\n" " *\n" " * @readonly\n" " * @since 0.1.5\n" " */\n" " name: \"Error\",\n\n" " /**\n" " * Error message\n" " *\n" " * @readonly\n" " * @since 0.1.5\n" " */\n" " message: \"\",\n\n" " /**\n" " * Build message by substituting context variables\n" " *\n" " * @param {Object} context Dictionary of named keys\n" " * @since 0.1.5\n" " */\n" " _buildMessage: function (context) {\n" " var replacements;\n" " if (context) {\n" " replacements = {};\n" " _gpfObjectForEach(context, function (value, key) {\n" " replacements[\"{\" + key + \"}\"] = value.toString();\n" " });\n" " this.message = _gpfStringReplaceEx(this.message, replacements);\n" " }\n" " }\n\n" "});\n\n" "function _gpfErrorFactory (code, name, message) {\n" " function NewErrorClass (context) {\n" " this._buildMessage(context);\n" " }\n" " NewErrorClass.prototype = new _GpfError();\n" " _gpfExtend(NewErrorClass.prototype, {\n" " code: code,\n" " name: name,\n" " message: message\n" " });\n" " // constructor can't be enumerated with wscript\n" " NewErrorClass.prototype.constructor = NewErrorClass;\n" " _GpfError[_gpfStringCapitalize(name)] = NewErrorClass;\n" " return function (context) {\n" " throw new NewErrorClass(context);\n" " };\n" "}\n\n" "/**\n" " * Generates an error class\n" " *\n" " * @param {Number} code Error code\n" " * @param {String} name Error name\n" " * @param {String} message Error message\n" " * @return {Function} New error class\n" " * @gpf:closure\n" " * @since 0.1.5\n" " */\n" "function _gpfGenenerateErrorFunction (code, name, message) {\n" " var result = _gpfErrorFactory(code, name, message);\n" " result.CODE = code;\n" " result.NAME = name;\n" " result.MESSAGE = message;\n" " return result;\n" "}\n\n" "// Last allocated error code\n" "var _gpfLastErrorCode = 0;\n\n" "/**\n" " * Declare error messages.\n" " * Each source declares its own errors.\n" " *\n" " * @param {String} source Source name\n" " * @param {Object} dictionary Dictionary of error name to message\n" " * @since 0.1.5\n" " */\n" "function _gpfErrorDeclare (source, dictionary) {\n" " _gpfIgnore(source);\n" " _gpfObjectForEach(dictionary, function (message, name) {\n" " var code = ++_gpfLastErrorCode;\n" " gpf.Error[\"CODE_\" + name.toUpperCase()] = code;\n" " gpf.Error[name] = _gpfGenenerateErrorFunction(code, name, message);\n" " });\n" "}\n\n" "_gpfErrorDeclare(\"error\", {\n" " /**\n" " * ### Summary\n" " *\n" " * Method or function is not implemented\n" " *\n" " * ### Description\n" " *\n" " * This error is used to flag methods or functions that are not yet implemented.\n" " * @since 0.1.5\n" " */\n" " notImplemented:\n" " \"Not implemented\",\n\n" " /**\n" " * ### Summary\n" " *\n" " * Method is abstract\n" " *\n" " * ### Description\n" " *\n" " * This error is used to implement abstract methods. Mostly used for interfaces.\n" " * @since 0.1.5\n" " */\n" " abstractMethod:\n" " \"Abstract method\",\n\n" " /**\n" " * ### Summary\n" " *\n" " * An assertion failed\n" " *\n" " * ### Description\n" " *\n" " * This error is triggered when an assertion fails\n" " *\n" " * @see {@link gpf.assert}\n" " * @see {@link gpf.asserts}\n" " * @since 0.1.5\n" " */\n" " assertionFailed:\n" " \"Assertion failed: {message}\",\n\n" " /**\n" " * ### Summary\n" " *\n" " * Method or function was called with an invalid parameter\n" " *\n" " * ### Description\n" " *\n" " * This error is used when a parameter is invalid\n" " * @since 0.1.5\n" " */\n" " invalidParameter:\n" " \"Invalid parameter\"\n" "});\n") matches = re.search(regex, test_str) if matches: print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group())) for groupNum in range(0, len(matches.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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 Python, please visit: https://docs.python.org/3/library/re.html