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

/
/
gm

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"\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$" test_str = ("/*!\n" " * jQuery JavaScript Library v3.6.0\n" " * https://jquery.com/\n" " *\n" " * Includes Sizzle.js\n" " * https://sizzlejs.com/\n" " *\n" " * Copyright OpenJS Foundation and other contributors\n" " * Released under the MIT license\n" " * https://jquery.org/license\n" " *\n" " * Date: 2021-03-02T17:08Z\n" " */\n" "//hellow orld\n" "( function( global, factory ) {\n" "/*\n" "Hell world\n" "*/\n" " \"use strict\";\n\n" " if ( typeof module === \"object\" && typeof module.exports === \"object\" ) {\n\n" " // For CommonJS and CommonJS-like environments where a proper `window`\n" " // is present, execute the factory and get jQuery.\n" " // For environments that do not have a `window` with a `document`\n" " // (such as Node.js), expose a factory as module.exports.\n" " // This accentuates the need for the creation of a real `window`.\n" " // e.g. var jQuery = require(\"jquery\")(window);\n" " // See ticket #14549 for more info.\n" " module.exports = global.document ?\n" " factory( global, true ) :\n" " function( w ) {\n" " if ( !w.document ) {\n" " throw new Error( \"jQuery requires a window with a document https://jquery.org/license\" );\n" " }\n" " return factory( w );\n" " };\n" " } else {\n" " factory( global );\n" " }\n\n" "// Pass this if window is not defined yet\n" "} )( typeof window !== \"undefined\" ? window : this, function( window, noGlobal ) {\n\n" "// Edge <= 12 - 13+, Firefox <=18 - 45+, IE 10 - 11, Safari 5.1 - 9+, iOS 6 - 9.1\n" "// throw exceptions when non-strict code (e.g., ASP.NET 4.5) accesses strict mode\n" "// arguments.callee.caller (trac-13335). But as of jQuery 3.0 (2016), strict mode should be common\n" "// enough that all such attempts are guarded in a try block.\n" "\"use strict\";\n\n" "var arr = [];\n\n" "var getProto = Object.getPrototypeOf;\n\n" "var slice = arr.slice;\n\n" "var flat = arr.flat ? function( array ) {\n" " return arr.flat.call( array );\n" "} : function( array ) {\n" " return arr.concat.apply( [], array );\n" "};\n\n\n" "var push = arr.push;\n\n" "var indexOf = arr.indexOf;\n\n" "var class2type = {};\n\n" "var toString = class2type.toString;\n\n" "var hasOwn = class2type.hasOwnProperty;\n\n" "var fnToString = hasOwn.toString;\n\n" "var ObjectFunctionString = fnToString.call( Object );\n\n" "var support = {};\n\n" "var isFunction = function isFunction( obj ) {\n\n" " // Support: Chrome <=57, Firefox <=52\n" " // In some browsers, typeof returns \"function\" for HTML <object> elements\n" " // (i.e., `typeof document.createElement( \"object\" ) === \"function\"`).\n" " // We don't want to classify *any* DOM node as a function.\n" " // Support: QtWeb <=3.8.5, WebKit <=534.34, wkhtmltopdf tool <=0.12.5\n" " // Plus for old WebKit, typeof returns \"function\" for HTML collections\n" " // (e.g., `typeof document.getElementsByTagName(\"div\") === \"function\"`). (gh-4756)\n" " return typeof obj === \"function\" && typeof obj.nodeType !== \"number\" &&\n" " typeof obj.item !== \"function\";\n" " };\n\n\n" "var isWindow = function isWindow( obj ) {\n" " return obj != null && obj === obj.window;\n" " };") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.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