Regular Expressions 101

Save & Share

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
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]
  • Character class intersection
    [\w&&[^\d]]
  • 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 re regex = re.compile(r"['\"]([^'\"]*\.(css|js|less|sass|txt|csv|jpeg|jpg|bmp|png|gif|json))['\"]", flags=re.MULTILINE | re.IGNORECASE) test_str = ("var styleSwitcher = {\n" " initialized: !1,\n" " defaults: {\n" " saveToStorage: !0,\n" " preserveCookies: !1,\n" " colorPrimary: \"#0088CC\",\n" " backgroundColor: \"light\",\n" " headerColor: \"light\",\n" " borderRadius: \"4px\",\n" " layoutStyle: \"wide\",\n" " sidebarColor: \"dark\",\n" " sidebarSize: \"md\",\n" " changeLogo: !0\n" " },\n" " initialize: function() {\n" " var a = this,\n" " b = $(\"html\").data(\"style-switcher-options\");\n" " this.initialized || (a.options = $.extend({}, a.defaults), String.prototype.capitalize = function() {\n" " return this.charAt(0).toUpperCase() + this.slice(1)\n" " }, jQuery.styleSwitcherCachedScript = function(a, b) {\n" " return b = $.extend(b || {}, {\n" " dataType: \"script\",\n" " cache: !0,\n" " url: a\n" " }), jQuery.ajax(b)\n" " }, null != $.cookie(\"borderRadius\") && (a.options.borderRadius = $.cookie(\"borderRadius\")), null != $.cookie(\"colorPrimary\") && (a.options.colorPrimary = \"#\" + $.cookie(\"colorPrimary\")), b && (b = b.replace(/'/g, '\"'), a.options = $.extend({}, a.options, JSON.parse(b)), a.options.preserveCookies = !0, a.options.saveToStorage = !1), $(\"head\").append($('<link rel=\"stylesheet\">').attr(\"href\", \"assets/vendor/style-switcher/style-switcher.css\")), $(\"head\").append($('<link rel=\"stylesheet/less\">').attr(\"href\", \"assets/vendor/style-switcher/less/skin.less\")), $(\"head\").append($('<link rel=\"stylesheet/less\">').attr(\"href\", \"assets/vendor/style-switcher/less/extension.less\")), $(\"head\").append($('<link rel=\"stylesheet\">').attr(\"href\", \"assets/vendor/style-switcher/bootstrap-colorpicker/css/bootstrap-colorpicker.css\")), $.styleSwitcherCachedScript(\"assets/vendor/style-switcher/bootstrap-colorpicker/js/bootstrap-colorpicker.min.js\").done(function(c, d) {\n" " less = {\n" " env: \"production\",\n" " modifyVars: {\n" " \"@border-radius\": a.options.borderRadius,\n" " \"@color-primary\": a.options.colorPrimary\n" " }\n" " }, $.styleSwitcherCachedScript(\"assets/vendor/less/less.min.js\").done(function(c, d) {\n" " $.ajax({\n" " url: \"assets/vendor/style-switcher/style.switcher.html\"\n" " }).done(function(c) {\n" " $(\"body\").append(c), a.container = $(\"#styleSwitcher\"), a.build(), a.events(), b || (null != $.cookie(\"layoutStyle\") && (a.options.layoutStyle = $.cookie(\"layoutStyle\")), null != $.cookie(\"backgroundColor\") && (a.options.backgroundColor = $.cookie(\"backgroundColor\")), null != $.cookie(\"headerColor\") && (a.options.headerColor = $.cookie(\"headerColor\")), null != $.cookie(\"sidebarColor\") && (a.options.sidebarColor = $.cookie(\"sidebarColor\")), null != $.cookie(\"sidebarSize\") && (a.options.sidebarSize = $.cookie(\"sidebarSize\"))), a.setLayoutStyle(a.options.layoutStyle), a.setBackgroundColor(a.options.backgroundColor), a.setHeaderColor(a.options.headerColor), a.setSidebarColor(a.options.sidebarColor), a.setSidebarSize(a.options.sidebarSize), a.setColors(), a.setBorderRadius(a.options.borderRadius), null == $.cookie(\"initialized\") && (a.container.find(\"#styleSwitcherOpen\").click(), $.cookie(\"initialized\", !0)), a.initialized = !0\n" " })\n" " })\n" " }), $.styleSwitcherCachedScript(\"assets/vendor/style-switcher/cssbeautify/cssbeautify.js\").done(function(a, b) {}))\n" " },\n" " build: function() {\n" " var a = this,\n" " b = a.container.find(\".color-primary input\");\n" " b.val(a.options.colorPrimary).parent().colorpicker({\n" " align: \"right\",\n" " customClass: \"style-switcher-color-picker\"\n" " }), $(\".colorpicker\").on(\"mousedown\", function(b) {\n" " b.preventDefault(), a.isChanging = !0\n" " }).on(\"mouseup\", function(c) {\n" " c.preventDefault(), a.isChanging = !1, a.options.colorPrimary = b.val(), a.setColors()\n" " }), $(\".colorpicker-element input\").on(\"blur\", function(c) {\n" " a.options.colorPrimary = b.val(), a.setColors()\n" " }), this.container.find(\".options-links.borders a\").click(function(b) {\n" " b.preventDefault(), a.setBorderRadius($(this).attr(\"data-border-radius\"))\n" " }), this.container.find(\".options-links.background-color a\").click(function(b) {\n" " b.preventDefault(), a.setBackgroundColor($(this).attr(\"data-background-color\"))\n" " }), this.container.find(\".options-links.header-color a\").click(function(b) {\n" " b.preventDefault(), a.setHeaderColor($(this).attr(\"data-header-color\"))\n" " }), this.container.find(\".options-links.sidebar-color a\").click(function(b) {\n" " b.preventDefault(), a.setSidebarColor($(this).attr(\"data-sidebar-color\"))\n" " }), this.container.find(\".options-links.layout a\").click(function(b) {\n" " b.preventDefault(), a.setLayoutStyle($(this).attr(\"data-layout-type\"))\n" " }), this.container.find(\".options-links.sidebar-size a\").click(function(b) {\n" " b.preventDefault(), a.setSidebarSize($(this).attr(\"data-sidebar-size\"))\n" " }), a.container.find(\".reset\").click(function(b) {\n" " b.preventDefault(), a.reset()\n" " }), a.container.find(\".get-css\").click(function(b) {\n" " b.preventDefault(), a.getCss()\n" " })\n" " },\n" " events: function() {\n" " var a = this;\n" " $(\"#styleSwitcherOpen\").click(function(b) {\n" " b.preventDefault(), a.container.hasClass(\"active\") ? a.container.animate({\n" " right: \"-\" + a.container.width() + \"px\"\n" " }, 300).removeClass(\"active\") : a.container.animate({\n" " right: \"0\"\n" " }, 300).addClass(\"active\")\n" " }), (null != $.cookie(\"showSwitcher\") || $(\"body\").hasClass(\"one-page\")) && ($(\"#styleSwitcherOpen\").click(), $.removeCookie(\"showSwitcher\"))\n" " },\n" " setColors: function(a, b) {\n" " var c = this;\n" " return this.isChanging ? !1 : (a && (c.options[\"color\" + b.capitalize()] = a, c.container.find(\".color-\" + b + \" input\").val(a)), c.options.preserveCookies || $.cookie(\"colorPrimary\", c.options.colorPrimary.replace(\"#\", \"\")), c.modifyVars(), void this.setLogo())\n" " },\n" " setBorderRadius: function(a) {\n" " var b = this;\n" " b.options.borderRadius = a, b.options.preserveCookies || $.cookie(\"borderRadius\", a), b.modifyVars();\n" " var c = this.container.find(\".options-links.borders\");\n" " c.find(\".active\").removeClass(\"active\"), c.find(\"a[data-border-radius=\" + a + \"]\").addClass(\"active\"), $.event.trigger({\n" " type: \"styleSwitcher.setBorderRadius\",\n" " radius: a\n" " })\n" " },\n" " setBackgroundColor: function(a) {\n" " var b = this;\n" " b.options.preserveCookies || $.cookie(\"backgroundColor\", a), this.options.saveToStorage && \"undefined\" != typeof localStorage && localStorage.setItem(\"backgroundColor\", a);\n" " var c = this.container.find(\".options-links.background-color\");\n" " c.find(\".active\").removeClass(\"active\"), c.find(\"a[data-background-color=\" + a + \"]\").addClass(\"active\"), \"dark\" == a ? ($(\"html\").addClass(\"dark\"), $(\"#addDarkClassInfo\").show()) : ($(\"html\").removeClass(\"dark\"), $(\"#addDarkClassInfo\").hide()), $.event.trigger({\n" " type: \"styleSwitcher.setBackgroundColor\",\n" " color: a\n" " }), this.setLogo()\n" " },\n" " setHeaderColor: function(a) {\n" " var b = this;\n" " b.options.preserveCookies || $.cookie(\"headerColor\", a), this.options.saveToStorage && \"undefined\" != typeof localStorage && localStorage.setItem(\"headerColor\", a);\n" " var c = this.container.find(\".options-links.header-color\");\n" " c.find(\".active\").removeClass(\"active\"), c.find(\"a[data-header-color=\" + a + \"]\").addClass(\"active\"), \"dark\" == a ? ($(\"html\").addClass(\"header-dark\"), $(\"#addHeaderDarkClassInfo\").show()) : ($(\"html\").removeClass(\"header-dark\"), $(\"#addHeaderDarkClassInfo\").hide()), $.event.trigger({\n" " type: \"styleSwitcher.setHeaderColor\",\n" " color: a\n" " })\n" " },\n" " setSidebarColor: function(a) {\n" " var b = this;\n" " b.options.preserveCookies || $.cookie(\"sidebarColor\", a), this.options.saveToStorage && \"undefined\" != typeof localStorage && localStorage.setItem(\"sidebarColor\", a);\n" " var c = this.container.find(\".options-links.sidebar-color\");\n" " c.find(\".active\").removeClass(\"active\"), c.find(\"a[data-sidebar-color=\" + a + \"]\").addClass(\"active\"), \"light\" == a ? ($(\"html\").addClass(\"sidebar-light\"), $(\"#addSidebarLightClassInfo\").show()) : ($(\"html\").removeClass(\"sidebar-light\"), $(\"#addSidebarLightClassInfo\").hide()), $.event.trigger({\n" " type: \"styleSwitcher.setSidebarColor\",\n" " color: a\n" " })\n" " },\n" " setLayoutStyle: function(a, b) {\n" " var c = this;\n" " if (c.options.preserveCookies || $.cookie(\"layoutStyle\", a), this.options.saveToStorage && \"undefined\" != typeof localStorage && localStorage.setItem(\"layout\", a), b) return $.cookie(\"showSwitcher\", !0), window.location.reload(), !1;\n" " var d = this.container.find(\".options-links.layout\");\n" " d.find(\".active\").removeClass(\"active\"), d.find(\"a[data-layout-type=\" + a + \"]\").addClass(\"active\"), \"wide\" == a ? ($(\"html\").removeClass(\"boxed\"), $(\"#addBoxedClassInfo\").hide()) : ($(\"html\").addClass(\"boxed\").removeClass(\"fixed\"), $(\"#addBoxedClassInfo\").show()), $.event.trigger({\n" " type: \"styleSwitcher.setLayoutStyle\",\n" " style: a\n" " })\n" " },\n" " setSidebarSize: function(a) {\n" " var b = this,\n" " c = $(\"html\");\n" " b.options.preserveCookies || $.cookie(\"sidebarSize\", a), this.options.saveToStorage && \"undefined\" != typeof localStorage && localStorage.setItem(\"sidebarSize\", a);\n" " var d = this.container.find(\".options-links.sidebar-size\");\n" " switch (d.find(\".active\").removeClass(\"active\"), d.find(\"a[data-sidebar-size=\" + a + \"]\").addClass(\"active\"), c.removeClass(\"sidebar-left-xs sidebar-left-sm\"), a) {\n" " case \"xs\":\n" " c.addClass(\"sidebar-left-xs\");\n" " break;\n" " case \"sm\":\n" " c.addClass(\"sidebar-left-sm\")\n" " }\n" " $.event.trigger({\n" " type: \"styleSwitcher.setSidebarSize\",\n" " color: a\n" " })\n" " },\n" " setLogo: function(a) {\n" " if (!this.options.changeLogo) return this;\n" " var b = $(\".header .logo img, .center-sign .logo img\");\n" " a || (\"#\" + $.cookie(\"colorPrimary\")).toUpperCase() == this.defaults.colorPrimary.toUpperCase() && \"dark\" != $.cookie(\"backgroundColor\") ? b.attr(\"src\", \"assets/images/logo-default.png\") : \"dark\" == $.cookie(\"backgroundColor\") ? b.attr(\"src\", \"assets/images/logo-light.png\") : b.attr(\"src\", \"assets/images/logo.png\"), $.event.trigger({\n" " type: \"styleSwitcher.setLogo\"\n" " })\n" " },\n" " modifyVars: function() {\n" " var a = this;\n" " less.modifyVars({\n" " \"@border-radius\": a.options.borderRadius,\n" " \"@color-primary\": a.options.colorPrimary\n" " }), this.options.saveToStorage && \"undefined\" != typeof localStorage && localStorage.setItem(\"skin-admin.css\", $('style[id^=\"less:\"]').text()), $.event.trigger({\n" " type: \"styleSwitcher.modifyVars\",\n" " options: a.options\n" " })\n" " },\n" " reset: function() {\n" " $.removeCookie(\"borderRadius\"), $.removeCookie(\"colorPrimary\"), $.removeCookie(\"backgroundColor\"), $.removeCookie(\"headerColor\"), $.removeCookie(\"layoutStyle\"), $.removeCookie(\"sidebarColor\"), $.removeCookie(\"sidebarSize\"), $.cookie(\"showSwitcher\", !0), window.location.reload(), \"undefined\" != typeof localStorage && (localStorage.removeItem(\"skin-admin.css\"), localStorage.removeItem(\"layout\"))\n" " },\n" " getCss: function() {\n" " var a = \"\",\n" " b = \"\";\n" " $(\"#getCSSTextarea\").text($('style[id$=\"less-skin\"]').text()).focus(function() {\n" " var a = $(this);\n" " a.select(), a.mouseup(function() {\n" " return a.unbind(\"mouseup\"), !1\n" " })\n" " }), a = $(\"#getCSSTextarea\").text(), $(\"#getCSSTextarea\").text(cssbeautify(a, {\n" " indent: \" \",\n" " autosemicolon: !0\n" " })), $(\"#getCSSTextareaExtension\").text($('style[id$=\"less-extension\"]').text()).focus(function() {\n" " var a = $(this);\n" " a.select(), a.mouseup(function() {\n" " return a.unbind(\"mouseup\"), !1\n" " })\n" " }), b = $(\"#getCSSTextareaExtension\").text(), $(\"#getCSSTextareaExtension\").text(cssbeautify(b, {\n" " indent: \" \",\n" " autosemicolon: !0\n" " })), $(\"#getCSSModal\").modal(\"show\")\n" " }\n" "};\n" "styleSwitcher.initialize();") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

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