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"<link rel=\"canonical\" href=\"(.*?)\">" test_str = ("const regex = /\"sku\": \"(.*?)\"/gm;\n\n" "// Alternative syntax using RegExp constructor\n" "// const regex = new RegExp('\"sku\": \"(.*?)\"', 'gm')\n\n" "const str = ``<!DOCTYPE html>\n" "<!-- saved from url=(0044)https://www.daz3d.com/karmen-9-hd-pro-bundle -->\n" "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\" class=\"\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n\n" "<title>Karmen 9 HD Pro Bundle | Daz 3D</title>\n" "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" "<meta name=\"description\" content=\"&lt;p&gt;Introducing the Karmen 9 HD Pro Bundle. This all-inclusive bundle features a vibrant makeup look, expressions, a curly and braided bun hairstyle, apocalyptic-inspired outfits, Elemental Mage and Shadow Realm attire, and even monstrous poses to immerse \">\n" "<meta name=\"keywords\" content=\"Karmen 9 HD Pro Bundle\">\n" "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" "<meta name=\"robots\" content=\"INDEX,FOLLOW,noai, noimageai\">\n" "<link rel=\"icon\" href=\"https://www.daz3d.com/static/images/favicon.ico\" type=\"image/x-icon\">\n" "<link rel=\"shortcut icon\" href=\"https://www.daz3d.com/static/images/favicon.ico\" type=\"image/x-icon\">\n" "<!--[if lt IE 7]>\n" "<script type=\"text/javascript\">\n" "//<![CDATA[\n" " var BLANK_URL = '/js/blank.html';\n" " var BLANK_IMG = '/js/spacer.gif';\n" "//]]>\n" "</script>\n" "<![endif]-->\n" "<link rel=\"stylesheet\" href=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/design-533ce80c63b6085aedc802b14d1faa79.css\" about=\"0\">\n" "<link rel=\"stylesheet\" href=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/product_zoom-5de5dd875434871413473d85f684dcfc.css\" about=\"1\">\n" "<style>\n" " @keyframes spinner {\n" " 0% {\n" " transform: translateY(-50%) rotate(0deg);\n" " }\n\n" " 100% {\n" " transform: translateY(-50%) rotate(360deg);\n" " }\n" " }\n\n" " .spinner::after {\n" " animation: 1.5s linear infinite spinner;\n" " animation-play-state: inherit;\n" " border: solid 3px #cfd0d1;\n" " border-bottom-color: #1c87c9;\n" " border-radius: 50%;\n" " content: \"\";\n" " height: 20px;\n" " width: 20px;\n" " display: inline-flex;\n" " flex-direction: column;\n" " padding-bottom: 5px;\n" " }\n" " </style>\n" "<script src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/getIds\" type=\"text/javascript\" async=\"\"></script><script type=\"text/javascript\" id=\"www-widgetapi-script\" src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/www-widgetapi.js.download\" async=\"\"></script><script async=\"\" src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/fbevents.js.download\"></script><script type=\"text/javascript\" async=\"\" src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/bat.js.download\"></script><script type=\"text/javascript\" async=\"\" src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/pixel.js.download\"></script><script type=\"text/javascript\" async=\"\" src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/analytics.js.download\"></script><script type=\"text/javascript\" async=\"\" src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/js\"></script><script type=\"text/javascript\" async=\"\" src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/destination\"></script><script src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/tag.js.download\" type=\"text/javascript\" async=\"\" id=\"cjapitag\"></script><script async=\"\" src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/affirm.js.download\"></script><script type=\"text/javascript\" async=\"\" src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/0\"></script><script src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/core-ed1ca31447e4f2e9bbf6d9ef38afe3c6.js.download\"></script>\n" "<style>\n" " @keyframes spinner {\n" " 0% {\n" " transform: translateY(-50%) rotate(0deg);\n" " }\n\n" " 100% {\n" " transform: translateY(-50%) rotate(360deg);\n" " }\n" " }\n\n" " .spinner::after {\n" " animation: 1.5s linear infinite spinner;\n" " animation-play-state: inherit;\n" " border: solid 3px #cfd0d1;\n" " border-bottom-color: #1c87c9;\n" " border-radius: 50%;\n" " content: \"\";\n" " height: 20px;\n" " width: 20px;\n" " display: inline-flex;\n" " flex-direction: column;\n" " padding-bottom: 5px;\n" " }\n" " </style>\n" "<script src=\"./Karmen 9 HD Pro Bundle _ Daz 3D_files/zoom-fee76dde1f2c855960b29036b231c115.js.download\"></script>\n" "<script type=\"application/ld+json\">{\n" " \"brand\": {\n" " \"name\": \"Daz 3D\",\n" " \"@type\": \"Brand\"\n" " },\n" " \"@context\": \"https://schema.org/\",\n" " \"description\": \"Introducing the Karmen 9 HD Pro Bundle. This all-inclusive bundle features a vibrant makeup look, expressions, a curly and braided bun hairstyle, apocalyptic-inspired outfits, Elemental Mage and Shadow Realm attire, and even monstrous poses to immerse your character into any narrative from modern desert to fantasy to horror.\",\n" " \"image\": \"https://gcdn.daz3d.com/p/91909/i/shimuzu_Karmen_Pro_Bundle.jpg\",\n" " \"name\": \"Karmen 9 HD Pro Bundle\",\n" " \"offers\": {\n" " \"availability\": \"https://schema.org/InStock\",\n" " \"itemCondition\": \"https://schema.org/NewCondition\",\n" " \"price\": 185.99,\n" " \"priceCurrency\": \"USD\",\n" " \"@type\": \"Offer\"\n" " },\n" " \"sku\": \"91909\",\n" " \"@type\": \"Product\"\n" "}</script>\n" "<link rel=\"canonical\" href=\"https://www.daz3d.com/karmen-9-hd-pro-bundle\">\n" "<script>\n" " if (!daz) { var daz = {}; };\n" " daz.api = new DazApi()\n\n" " from 331.2deg,\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1)\n" " )\n" " 1;\n" " }\n" " 93% {\n" " border-image: conic-gradient(\n" " from 334.8deg,\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1)\n" " )\n" " 1;\n" " }\n" " 94% {\n" " border-image: conic-gradient(\n" " from 338.4deg,\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1)\n" " )\n" " 1;\n" " }\n" " 95% {\n" " border-image: conic-gradient(\n" " from 342deg,\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1)\n" " )\n" " 1;\n" " }\n" " 96% {\n" " border-image: conic-gradient(\n" " from 345.6deg,\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1)\n" " )\n" " 1;\n" " }\n" " 97% {\n" " border-image: conic-gradient(\n" " from 349.2deg,\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1)\n" " )\n" " 1;\n" " }\n" " 98% {\n" " border-image: conic-gradient(\n" " from 352.8deg,\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1)\n" " )\n" " 1;\n" " }\n" " 99% {\n" " border-image: conic-gradient(\n" " from 356.4deg,\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1)\n" " )\n" " 1;\n" " }\n" " 100% {\n" " border-image: conic-gradient(\n" " from 360deg,\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1),\n" " var(--border-color-2),\n" " var(--border-color-1)\n" " )\n" " 1;\n" " }\n" " }\n" " }\n\n" " /* END rotate bonus box border */\n" " /* BEGIN falling stars animation */\n\n" " .bg-stars {\n" " width: 100%;\n" " height: 100%;\n" " position: absolute;\n" " top: 0;\n" " left: 0;\n" " overflow: hidden;\n" " }\n\n" " .bg-stars .bg-star {\n" " font-size: 1em;\n" " position: absolute;\n" " top: -25px;\n" " color: yellow;\n" " user-select: none;\n" " cursor: default;\n" " text-shadow: 0 0 1px #000;\n" " animation-name: fallingStars;\n" " animation-duration: 5s;\n" " animation-timing-function: linear;\n" " animation-iteration-count: infinite;\n" " animation-play-state: running;\n" " z-index: -1;\n" " }\n" " \n" " .bg-stars .bg-star:nth-of-type(1) {\n" " left: 10%;\n" " -webkit-animation-delay: 1s, 1s;\n" " animation-delay: 1s, 1s;\n" " }\n\n" " .bg-stars .bg-star:nth-of-type(2) {\n" " left: 20%;\n\n\n" "</div></body></html>(`;\n" "let m;\n\n" "while ((m = regex.exec(str)) !== null) {\n" " // This is necessary to avoid infinite loops with zero-width matches\n" " if (m.index === regex.lastIndex) {\n" " regex.lastIndex++;\n" " }\n" " // The result can be accessed through the `m`-variable.\n" " m.forEach((match, groupIndex) => {\n" " console.log(`Found match, group ${groupIndex}: ${match}`);\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