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
No Match

/
/
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"<a\s+.*?href=\"https:\/\/code.amazon.com\/packages\/AWSBrewPricingPlcConsole\/releases\/(\d+)\.(\d+)\.(\d+)\.(\d+)" test_str = ("\n\n\n\n\n" "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" " \"http://www.w3.org/TR/html4/strict.dtd\">\n" "<html>\n\n" "<head>\n" " <title>\n" " AWSBrewPricingPlcConsole - Releases - Brazil Directory\n" " </title>\n\n" " \n\n" "<!-- EXPERIMENTAL: Subject to change, don't rely on these -->\n" "<meta name=\"environment\" content=\"DevCentralWebsite\">\n" "<meta name=\"stage\" content=\"Prod\">\n" "<!-- /EXPERIMENTAL -->\n\n\n" " <link rel=\"stylesheet\"\n" " type=\"text/css\"\n" " href=\"https://internal-cdn.amazon.com/dtux.amazon.com/stylesheets/dtux-style.css\">\n\n" " <link href=\"https://internal-cdn.amazon.com/devcentral.amazon.com/style/helptips.css\"\n" " rel=\"stylesheet\"\n" " type=\"text/css\">\n\n" " <link rel=\"icon\"\n" " href=\"https://internal-cdn.amazon.com/devcentral.amazon.com/images/brazil.ico\"\n" " type=\"image/x-icon\">\n" " <link rel=\"shortcut icon\"\n" " href=\"https://internal-cdn.amazon.com/devcentral.amazon.com/images/brazil.ico\"\n" " type=\"image/x-icon\">\n\n" " <link rel=\"stylesheet\"\n" " type=\"text/css\"\n" " href=\"https://internal-cdn.amazon.com/btk.amazon.com/ajax/libs/jquery/plugins/autocomplete/1.1/jquery.autocomplete.min.css\">\n\n" " <link href=\"https://internal-cdn.amazon.com/devcentral.amazon.com/brazil/directory/stylesheet.min.css?size=24097\"\n" " rel=\"stylesheet\"\n" " type=\"text/css\">\n\n" " <link rel=\"stylesheet\" type=\"text/css\" href=\"https://internal-cdn.amazon.com/devcentral.amazon.com/brazil/directory/visualizer/stylesheets/visualizer.min.css?size=1088\" >\n\n" " <link rel=\"stylesheet\"\n" " type=\"text/css\"\n" " href=\"https://internal-cdn.amazon.com/is-it-down.amazon.com/stylesheets/stripe.css\">\n\n" " <script type=\"text/javascript\"\n" " src=\"https://internal-cdn.amazon.com/btk.amazon.com/ajax/libs/jquery/1.7.2/jquery.min.js\"></script>\n" " <script type=\"text/javascript\"\n" " src=\"https://internal-cdn.amazon.com/dtux.amazon.com/javascripts/cache/dtux-style.js\"></script>\n" " <script type=\"text/javascript\"\n" " src=\"https://internal-cdn.amazon.com/btk.amazon.com/ajax/libs/jquery/plugins/autocomplete/1.1/jquery.autocomplete.min.js\"></script>\n" " <script type=\"text/javascript\"\n" " src=\"https://internal-cdn.amazon.com/btk.amazon.com/ajax/libs/jquery/plugins/tablesorter/2.0.3/jquery.tablesorter.min.js\"></script>\n\n" " <script type=\"text/javascript\"\n" " src=\"https://internal-cdn.amazon.com/devcentral.amazon.com/brazil/directory/script.min.js?size=2302\">\n" " </script>\n\n" " <script type=\"text/javascript\">\n" " IS_PROD = true;\n" " REMOTE_USER = 'vladyang';\n" " </script>\n\n" "</head>\n\n" "<body>\n" "<div id=\"content\">\n" " <div class=\"inner-container\">\n\n" " <div class=\"indexHeader\">\n" " <span class='titleBarTitle'>\n" " \n" "<a href=\"/\">devcentral</a> &gt;\n\n" " <a href=\"https://devcentral.amazon.com/ac/brazil/directory/\">brazil directory</a>\n" " &gt; \n" " <a href=\"https://devcentral.amazon.com/ac/brazil/directory/packages/by-owner\">packages</a> &gt; <a href=\"https://devcentral.amazon.com/ac/brazil/directory/package/overview/AWSBrewPricingPlcConsole\">AWSBrewPricingPlcConsole</a> &gt; <a href=\"https://devcentral.amazon.com/ac/brazil/directory/package/majorVersionSummary/AWSBrewPricingPlcConsole?majorVersion=1.3\">1.3</a> &gt; release notes\n\n" " </span>\n" " </div>\n\n" " <div class='titleBarHelper'>\n" " \n" " <div id=\"searchBoxes\">\n" " \n" " \n" " <form action=\"https://devcentral.amazon.com/ac/brazil/directory/packages/search\"\n" " id=\"findPackageForm\">\n" " <div>\n" " Search Packages:\n" " <input type=\"text\" id=\"findPackage\" name=\"q\">\n" " </div>\n" " </form>\n\n" " \n" " \n" " <form action=\"https://devcentral.amazon.com/ac/brazil/directory/version-sets/search\"\n" " id=\"findVersionSetForm\">\n" " <div>\n" " Search Version Sets:\n" " <input type=\"text\"\n" " id=\"findVersionSet\"\n" " class=\"versionSetInput\"\n" " name=\"q\"\n" " value=\"live\">\n" " </div>\n" " </form>\n\n" " </div>\n\n" " </div>\n\n" " <h1>Package: AWSBrewPricingPlcConsole</h1>\n\n" " \n" " \n\n" "<!--\n" "Timing for addPackageToRoute (in seconds):\n" "{\n" " 'getBranchByPackageAndName' => '0.0113730430603027',\n" " 'getPackageSynopsisByName' => '0.0839250087738037'\n" "}\n\n" "-->\n\n" "<div id=\"viewInLinks\">\n" "View in\n" " <a href=\"/brazil/go/src-control?pkg=AWSBrewPricingPlcConsole\">Code Browser</a>\n" " |\n" " <a href=\"https://omni-grok.amazon.com/s?path=/AWSBrewPricingPlcConsole&indexName=GitFarm_6\">OmniGrok</a>\n\n" " |\n" " <a href=\"https://apollo.amazon.com/package.html?packageName=AWSBrewPricingPlcConsole&amp;showEnvironmentUsage=1\">\n" " Apollo\n" " </a>\n" "</div>\n\n\n" " <div id=\"headerWarning\">\n" " \n\n" " </div>\n\n" " <div id=\"flash\">\n" " \n" " \n" " \n\n\n" " </div>\n\n" " \n" " \n" " \n" "<div class=\"tabs has-subnav\">\n" "<ul>\n" " <li class=\"tab \"><a class=\" \"\n" " href=\"https://devcentral.amazon.com/ac/brazil/directory/package/overview/AWSBrewPricingPlcConsole\">Overview</a></li>\n" " <li class=\"tab \"><a class=\" \"\n" " href=\"https://devcentral.amazon.com/ac/brazil/directory/package/branches/AWSBrewPricingPlcConsole\">Branches</a></li>\n" " <li class=\"tab \"><a class=\" \"\n" " href=\"https://devcentral.amazon.com/ac/brazil/directory/package/releases/AWSBrewPricingPlcConsole\">All Releases</a></li>\n" " <li class=\"tab \"><a class=\" \"\n" " href=\"https://devcentral.amazon.com/ac/brazil/directory/package/sourceGuidance/AWSBrewPricingPlcConsole\">Source Guidance</a></li>\n" " <li class=\"tab active\"><a class=\"active vendor-guidance-NONE\"\n" " href=\"https://devcentral.amazon.com/ac/brazil/directory/package/majorVersionSummary/AWSBrewPricingPlcConsole?majorVersion=1.3\">1.3 </a></li>\n" " <li class=\"tab \"><a class=\" vendor-guidance-NONE\"\n" " href=\"https://devcentral.amazon.com/ac/brazil/directory/package/majorVersionSummary/AWSBrewPricingPlcConsole?majorVersion=1.0\">1.0 </a></li>\n" "</ul>\n" "</div>\n\n" " \n" " \n" "<div class=\"subtabs\">\n" "<ul>\n" " <li class=\"subtab \"><a class=\" \"\n" " href=\"https://devcentral.amazon.com/ac/brazil/directory/package/majorVersionSummary/AWSBrewPricingPlcConsole?majorVersion=1.3\">Summary</a></li>\n" " <li class=\"subtab active\"><a class=\"active \"\n" " href=\"https://devcentral.amazon.com/ac/brazil/directory/package/notes/AWSBrewPricingPlcConsole?majorVersion=1.3\">Releases</a></li>\n" " <li class=\"subtab \"><a class=\" \"\n" " href=\"https://devcentral.amazon.com/ac/brazil/directory/package/deps/AWSBrewPricingPlcConsole?direction=down&majorVersion=1.3&graphType=runtime_dependencies-resolved\">Dependencies</a></li>\n" " <li class=\"subtab \"><a class=\" \"\n" " href=\"https://devcentral.amazon.com/ac/brazil/directory/package/deps/AWSBrewPricingPlcConsole?direction=up&majorVersion=1.3\">Consumers</a></li>\n" "</ul>\n" "</div>\n\n\n\n\n" " <div class='underNav'>\n" " <div class=\"subNavHolder\">\n\n\n\n\n\n\n\n\n\n" "<div id=\"actions\">\n" " <h3>Package Actions</h3>\n\n\n" " \n" "<form method=\"get\"\n" " action=\"/brazil/packages/versionDiff/\">\n" " <div>\n" " <input type=\"hidden\" name=\"packageName\" value=\"AWSBrewPricingPlcConsole\">\n" " <button type=\"submit\">\n" " Diff <b>Versions</b>\n" " </button>\n" " </div>\n" "</form>\n\n" "</div>\n" " \n\n\n\n\n\n\n\n\n" "<h2>Releases</h2>\n\n" "<div class=\"releaseNotes\">\n" "<table class=\"releaseNotes\">\n" " <tr>\n" " <td class=\"version\">\n" " <a name=\"1.3.3.0\"></a><a href=\"https://code.amazon.com/packages/AWSBrewPricingPlcConsole/releases/1.3.3.0\"><b>1.3</b>.3.0</a>\n" " </td>\n" " <td class=\"nowrap\">Tue Jan 8 01:39:08 2019</td>\n" " <td>\n" " AWSBrewPricingPlcConsole/<a href=\"/brazil/go/src-control?branch=mainline&pkg=AWSBrewPricingPlcConsole\">mainline</a>@<a href=\"/brazil/go/src-control?branch=mainline&cln=6a10a7b2e3108941009efcfaef877b0df6a4063a&pkg=AWSBrewPricingPlcConsole\">6a10a7b2e3108941009efcfaef877b0df6a4063a</a>&nbsp;(GitFarm)\n" " </td>\n" " <td>\n" " <a href=\"https://devcentral.amazon.com/ac/brazil/directory/version-set/overview/AWSBrewPricingPlcTools/AWSBrewPricingPlcConsole\">AWSBrewPricingPlcTools/AWSBrewPricingPlcConsole</a>\n" " </td>\n" " <td style=\"width: 50%\">\n" " Built in <a href=\"https://build.amazon.com/2058367784\">request 2058367784</a>\n" " </td>\n\n" " </tr>\n" " <tr>\n" " <td colspan=\"5\" class=\"text\">\n" " <div style=\"overflow: auto\">\n" " Requester: vladyang<br>\n" " [Change 6a10a7b2e3108941009efcfaef877b0df6a4063a by vladyang] adding version info<br>\n" "<br>\n\n" " </div>\n" " </td>\n" " </tr>\n" " <tr>\n" " <td class=\"version\">\n" " <a name=\"1.3.2.0\"></a><a href=\"https://code.amazon.com/packages/AWSBrewPricingPlcConsole/releases/1.3.2.0\"><b>1.3</b>.2.0</a>\n" " </td>\n" " <td class=\"nowrap\">Tue Jan 8 01:20:29 2019</td>\n" " <td>\n" " AWSBrewPricingPlcConsole/<a href=\"/brazil/go/src-control?branch=mainline&pkg=AWSBrewPricingPlcConsole\">mainline</a>@<a href=\"/brazil/go/src-control?branch=mainline&cln=3d728050bff78d98870ca70662e62bebbfa91f48&pkg=AWSBrewPricingPlcConsole\">3d728050bff78d98870ca70662e62bebbfa91f48</a>&nbsp;(GitFarm)\n" " </td>\n" " <td>\n" " <a href=\"https://devcentral.amazon.com/ac/brazil/directory/version-set/overview/AWSBrewPricingPlcTools/AWSBrewPricingPlcConsole\">AWSBrewPricingPlcTools/AWSBrewPricingPlcConsole</a>\n" " </td>\n" " <td style=\"width: 50%\">\n" " Built in <a href=\"https://build.amazon.com/2058362893\">request 2058362893</a>\n" " </td>\n\n" " </tr>\n" " <tr>\n" " <td colspan=\"5\" class=\"text\">\n" " <div style=\"overflow: auto\">\n" " Requester: vladyang<br>\n" " [3d72805 by vladyang] Initial Test Set\n" " </div>\n" " </td>\n" " </tr>\n" " <tr>\n" " <td class=\"version\">\n" " <a name=\"1.3.1.0\"></a><a href=\"https://code.amazon.com/packages/AWSBrewPricingPlcConsole/releases/1.3.1.0\"><b>1.3</b>.1.0</a>\n" " </td>\n" " <td class=\"nowrap\">Tue Jan 8 00:50:19 2019</td>\n" " <td>\n" " AWSBrewPricingPlcConsole/<a href=\"/brazil/go/src-control?branch=refactorings&pkg=AWSBrewPricingPlcConsole\">refactorings</a>@<a href=\"/brazil/go/src-control?branch=refactorings&cln=afcb73f4968c4fc6baf6beb85f8414ad1a2ab603&pkg=AWSBrewPricingPlcConsole\">afcb73f4968c4fc6baf6beb85f8414ad1a2ab603</a>&nbsp;(GitFarm)\n" " </td>\n" " <td>\n" " <a href=\"https://devcentral.amazon.com/ac/brazil/directory/version-set/overview/AWSBrewPricingPlcTools/AWSBrewPricingPlcConsole\">AWSBrewPricingPlcTools/AWSBrewPricingPlcConsole</a>\n" " </td>\n" " <td style=\"width: 50%\">\n" " Built in <a href=\"https://build.amazon.com/2058354122\">request 2058354122</a>\n" " </td>\n\n" " </tr>\n" " <tr>\n" " <td colspan=\"5\" class=\"text\">\n" " <div style=\"overflow: auto\">\n" " Requester: vladyang<br>\n" " [afcb73f by vladyang] Fixing package versioning\n" " </div>\n" " </td>\n" " </tr>\n" "</table>\n" "</div>\n\n" "<a name=\"end\"></a>\n\n\n" "<script type=\"text/javascript\"\n" " src=\"https://internal-cdn.amazon.com/devcentral.amazon.com/brazil/directory/js/package.min.js?size=1292\"></script>\n\n\n\n\n" " <div style=\"clear:both\"></div>\n" " </div>\n" " </div>\n" " </div>\n" "</div>\n\n" " <div id='footer'>\n" " <div class='column'>\n" " <h3>Packages</h3>\n" " <ul>\n" " <li><a href=\"https://octane.amazon.com/package\">Create Package</a></li>\n" " <li><a href=\"/brazil/packages\">Third Party Packages</a></li>\n" " <li><a href=\"https://code.amazon.com/packages/find_by_team_for_user\">Packages By Owner</a></li>\n" " <li><a href=\"/ac/brazil/directory/packages/prune-list\">Pruned Packages</a></li>\n" " </ul>\n" " </div>\n" " <div class='column'>\n" " <h3>Version Sets</h3>\n" " <ul>\n" " <li><a href=\"https://code.amazon.com/version-sets/new\">Create</a></li>\n" " <li><a href=\"https://code.amazon.com/version-sets/\">Browse</a></li>\n" " </ul>\n" " </div>\n" " <div class='business_card'>\n" " <h3>Need help?</h3>\n" " <ul>\n" " <li><a href=\"https://w.amazon.com/index.php/BuilderTools/Product/Brazil_Package_Directory\">Package Directory Documentation</a></li>\n" " <li><a href=\"https://i.amazon.com/issues/create?assignedFolder=27de5d2d-8357-4fe4-9344-7f9d95d94d6b&descriptionContentType=text%2Famz-markdown-sim&extensions%5Btt%5D%5Bimpact%5D=4\">Submit an Issue (problems or suggestions)</a></li>\n" " <li><a href=\"https://w.amazon.com/index.php/BuilderTools/OfficeHours\">Office Hours</a></li>\n" " </ul>\n" " <div class=\"browser_support\">\n" " <a href=\"https://w.amazon.com/?BuilderTools/Browser_Support_Policy\">Browser Support Policy</a>\n" " </div>\n" " </div>\n" " </div>\n\n" " \n" "<script type=\"text/javascript\" src=\"https://internal-cdn.amazon.com/devcentral.amazon.com/js/display.min.js?size=582\"></script>\n\n\n" " <script type=\"text/javascript\" src=\"https://internal-cdn.amazon.com/is-it-down.amazon.com/javascripts/stripe.min.js\"></script>\n" " <script type=\"text/javascript\">\n" " // \"Is It Down?\" Stripe for Package Builder\n" " isItDownStripe(\"brazil\", 2, 1);\n" " </script>\n\n" " </body>\n" "</html>\n\n\n\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