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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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

/
/
g

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"\{{(\w+)\}}" test_str = ("\n" "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" "<html lang=\"en\" xml:lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:v=\"urn:schemas-microsoft-com:vml\">\n" "<head>\n" " <meta content=\"text/html; charset=UTF-8\" http-equiv=\"Content-Type\" />\n" " <meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\" />\n" " <meta name=\"x-apple-disable-message-reformatting\" /><!--[if mso]>\n" " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />\n" " <![endif]-->\n" " <title>Scheduled Maintenance</title>\n" " <style type=\"text/css\">/* === Custom Fonts === */\n" " /* Add your fonts here via imports */\n\n" " /* === Client Styles === */\n" " #outlook a {padding: 0;}\n" " .ReadMsgBody {width: 100%;} .ExternalClass {width: 100%;}\n" " .ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div {line-height: 100%;} \n" " body, table, td, p, a, li, blockquote {-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;}\n" " table, td {mso-table-lspace: 0pt; mso-table-rspace: 0pt;}\n" " img {-ms-interpolation-mode: bicubic;}\n\n" " /* === Reset Styles === */\n" " body, p, h1, h3 {margin: 0; padding: 0;}\n" " img {border: 0; display: block; height: auto; line-height: 100%; max-width: 100%; outline: none; text-decoration: none;}\n" " table, td {border-collapse: collapse}\n" " body {height: 100% !important; margin: 0; padding: 0; width: 100% !important;}\n\n" " /* === Page Structure === */\n" " /*\n" " Set the background color of your email. Light neutrals or your primary brand color are most common.\n" " */\n" " body {\n" " background-color: #2C3E50; /* Edit */\n" " }\n\n" " /*\n" " This optional section will be hidden in your email but the text will appear after the subject line. \n" " */\n" " #preheader {display: none !important; font-size: 1px; line-height: 1px; max-height: 0px; max-width: 0px; mso-hide: all !important; opacity: 0; overflow: hidden; visibility: hidden;}\n\n" " /*\n" " Set the background color, border and radius of your primary content area. White or light neutrals for the background-color are recommended.\n" " */\n" " .panel-container {\n" " background-color: #E1F5FE; /* Edit */\n" " border: 1px solid #eaebec; /* Edit */\n" " border-collapse: separate;\n" " border-radius: 2px; /* Edit */\n" " }\n\n" " /*\n" " Set the horizontal padding of your content areas. Any changes should following the default spacing scale.\n" " */ \n" " #header, #footer {padding-left: 32px; padding-right: 32px;}\n" " .panel-body {padding-left: 32px; padding-right: 32px;}\n\n" " /*\n" " Set the sizes of your spacer rows. Spacers are used for vertical padding. Any changes should following the default spacing scale.\n" " */\n" " .spacer-xxs, .spacer-xs, .spacer-sm, .spacer-md, .spacer-lg, .spacer-xl, .spacer-xxl {display: block; width: 100%;}\n" " .spacer-xxs {height: 4px; line-height: 4px;}\n" " .spacer-xs {height: 8px; line-height: 8px;}\n" " .spacer-sm {height: 16px; line-height: 16px;}\n" " .spacer-md {height: 24px; line-height: 24px;}\n" " .spacer-lg {height: 32px; line-height: 32px;}\n" " .spacer-xl {height: 40px; line-height: 40px;}\n" " .spacer-xxl {height: 48px; line-height: 48px;}\n" " \n" " /* === Page Styles === */\n" " /*\n" " Set the font-family of your type. Classes should be set directly on the table cell for compatibility with older clients. Any changes should follow the default typography scale.\n" " */\n" " .headline-one, .headline-two, .headline-three, .heading, .subheading, .body, .caption, .button, .table-heading {\n" " font-family: -apple-system,system-ui,BlinkMacSystemFont,\"Segoe UI\",Roboto,\"Helvetica Neue\",Arial,sans-serif; /* Edit */\n" " font-style: normal;\n" " font-variant: normal;\n" " }\n" " .headline-one {font-size: 32px; font-weight: 500; line-height: 40px;}\n" " .headline-two {font-size: 24px; font-weight: 500; line-height: 32px;}\n" " .headline-three {font-size: 20px; font-weight: 500; line-height: 24px;}\n" " .heading {font-size: 16px; font-weight: 500; line-height: 24px;}\n" " .subheading {font-size: 12px; font-weight: 700; line-height: 16px; text-transform: uppercase;}\n" " .body {font-size: 14px; font-weight: 400; line-height: 20px;}\n" " .caption {font-size: 12px; font-weight: 400; line-height: 16px;}\n" " .table-heading {font-size: 10px; font-weight: 700; text-transform: uppercase;}\n\n" " /*\n" " Set the styles of your links.\n" " */\n" " a {color: inherit; font-weight: normal; text-decoration: underline;}\n\n" " /*\n" " Set the colors of your text.\n" " */ \n" " .text-primary {\n" " color: #007bff; /* Edit */\n" " }\n" " .text-secondary {\n" " color: #ECF0F1 ; /* Edit */\n" " }\n" " .text-black {\n" " color: #000000; /* Edit */\n" " }\n" " .text-dark-gray {\n" " color: #343a40; /* Edit */\n" " }\n" " .text-gray {\n" " color: #6c757d; /* Edit */\n" " }\n" " .text-light-gray {\n" " color: #f8f9fa; /* Edit */\n" " }\n" " .text-white {\n" " color: #ffffff; /* Edit */\n" " }\n" " .text-success {\n" " color: #28a745; /* Edit */\n" " }\n" " .text-danger {\n" " color: #dc3545; /* Edit */\n" " }\n" " .text-warning {\n" " color: #ffc107; /* Edit */\n" " }\n" " .text-info {\n" " color: #17a2b8; /* Edit */\n" " }\n\n" " /*\n" " Set the styles of your buttons. Each button requires a matching background.\n" " */\n" " .button-bg {\n" " border-radius: 2px; /* Editable */\n" " }\n" " .button-bg-primary {\n" " background-color: #007bff /* Editable */;\n" " }\n" " .button-bg-secondary {\n" " background-color: #6c757d; /* Editable */\n" " }\n" " .button-bg-success {\n" " background-color: #28a745; /* Editable */\n" " }\n" " .button-bg-danger {\n" " background-color: #dc3545; /* Editable */\n" " }\n" " .button {\n" " border-radius: 2px; /* Editable */\n" " color: #ffffff; /* Editable */\n" " display: inline-block;\n" " font-size: 14px;\n" " font-weight: 700; \n" " padding: 10px 20px 10px;\n" " text-decoration: none;\n" " }\n" " .button-primary {\n" " border: 1px solid #007bff /* Editable */;\n" " }\n" " .button-secondary {\n" " border: 1px solid #6c757d; /* Editable */\n" " }\n" " .button-success {\n" " border: 1px solid #28a745; /* Editable */\n" " } \n" " .button-danger {\n" " border: 1px solid #dc3545; /* Editable */\n" " }\n\n" " /*\n" " Set the styles of your backgrounds.\n" " */ \n" " .bg {padding-left: 24px; padding-right: 24px;} \n" " .bg-primary {\n" " background-color: #007bff; /* Edit */\n" " }\n" " .bg-secondary {\n" " background-color: #6c757d; /* Edit */\n" " }\n" " .bg-black {\n" " background-color: #000000; /* Edit */\n" " }\n" " .bg-dark-gray {\n" " background-color: #343a40; /* Edit */\n" " }\n" " .bg-gray {\n" " background-color: #6c757d; /* Edit */\n" " }\n" " .bg-light-gray {\n" " background-color: #f8f9fa; /* Edit */\n" " }\n" " .bg-white {\n" " background-color: #ffffff; /* Edit */\n" " }\n" " .bg-success {\n" " background-color: #28a745; /* Edit */\n" " }\n" " .bg-danger {\n" " background-color: #dc3545; /* Edit */\n" " }\n" " .bg-warning {\n" " background-color: #ffc107; /* Edit */\n" " }\n" " .bg-info {\n" " background-color: #17a2b8; /* Edit */\n" " }\n\n" " /*\n" " Set the styles of your tabular information. This class should not be set on tables with a role of presentation.\n" " */\n" " .table {min-width: 100%; width: 100%;}\n" " .table td {\n" " border-top: 1px solid #eaebec; /* Editable */\n" " padding-bottom: 12px;\n" " padding-left: 12px;\n" " padding-right: 12px;\n" " padding-top: 12px;\n" " vertical-align: top;\n" " }\n" " \n" " /*\n" " Set the styles of your utility classes.\n" " */\n" " .address, .address a {color: inherit !important;}\n" " .border-solid {\n" " border-style: solid !important;\n" " border-width: 2px !important; /* Edit */\n" " border-color: #eaebec !important; /* Edit */\n" " }\n" " .divider {\n" " border-bottom: 0px; \n" " border-top: 1px solid #eaebec; /* Edit */\n" " height: 1px; \n" " line-height: 1px;\n" " width: 100%;\n" " } \n" " .text-bold {font-weight: 700;}\n" " .text-italic {font-style: italic;}\n" " .text-uppercase {text-transform: uppercase;}\n" " .text-underline {text-decoration: underline;}\n\n" " @media only screen and (max-width: 599px) \n" " {\n" " /* === Client Styles === */ \n" " body, table, td, p, a, li, blockquote {-webkit-text-size-adjust: none !important;}\n" " body {min-width: 100% !important; width: 100% !important;}\n" " center {padding-left: 12px !important; padding-right: 12px !important;}\n\n" " /* === Page Structure === */\n" " /*\n" " Adjust sizes and spacing on mobile.\n" " */\n" " #email-container {max-width: 600px !important; width: 100% !important;}\n" " #header, #footer {padding-left: 24px !important; padding-right: 24px !important;}\n" " .panel-container {max-width: 600px !important; width: 100% !important;} \n" " .panel-body {padding-left: 24px !important; padding-right: 24px !important;}\n" " .column-responsive {display: block !important; padding-bottom: 24px !important; width:100% !important;}\n" " .column-responsive img {width: auto !important;}\n" " .column-responsive-last {padding-bottom: 0px !important;}\n" " .column-responsive-gutter {display: none !important;}\n\n" " /* === Page Styles === */\n" " /*\n" " Adjust sizes and spacing on mobile.\n" " */\n" " }\n" " </style>\n" " <!--[if gte mso 9]>\n" " <xml>\n" " <o:OfficeDocumentSettings>\n" " <o:AllowPNG/>\n" " <o:PixelsPerInch>96</o:PixelsPerInch>\n" " </o:OfficeDocumentSettings>\n" " </xml>\n" " <![endif]--><!--[if mso]>\n" " <xml xmlns:w=\"urn:schemas-microsoft-com:office:word\">\n" " <w:WordDocument><w:AutoHyphenation/></w:WordDocument>\n" " </xml>\n" " <![endif]-->\n" "</head>\n" "<body style=\"background-color:#2C3E50;\">\n" "<center><!-- Start Email Container -->\n\n" "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"email-container\" role=\"presentation\" width=\"600\" >\n" " <tbody><!-- Start Preheader -->\n" " <tr bgcolor=>\n" " <td id=\"preheader\">SMS received</td>\n" " </tr>\n" " <!-- End Preheader -->\n" " <tr>\n" " <td class=\"spacer-lg\"></td>\n" " </tr>\n" " <tr>\n" " <td id=\"email-body\" valign=\"top\"><!-- Start Panel Container -->\n" " <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"panel-container\" role=\"presentation\" width=\"100%\" bgcolor=\"#E1F5FE\">\n" " <tbody>\n" " <tr>\n" " <td class=\"spacer-lg\"></td>\n" " </tr>\n" " <!-- Start Header -->\n" " <tr>\n" " <td align=\"left\" id=\"header\"><a href=\"https://gem-car.com\"><img alt=\"Company\" border=\"0\" src=\"https://www.gem-car.com/images/stories/logo/gem-car-billing-software-automobile-garage.png\" /> </a></td> \n" " </tr>\n" " <tr>\n" " <td align=\"left\" class=\"headline-two text-dark-gray\">{{Organization}}</td>\n" " </tr>\n" " <!-- End Header -->\n" " <tr>\n" " <td class=\"spacer-lg\"></td>\n" " </tr>\n" " <tr>\n" " <td class=\"panel-body\">\n" " <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" role=\"presentation\" width=\"100%\">\n" " <tbody><!-- Start Text -->\n" " <tr>\n" " <td align=\"left\" class=\"headline-one text-dark-gray\">Your Monthly Email statistics for {{MonthName}}</td>\n" " </tr>\n" " <tr>\n" " <td class=\"spacer-md\"></td>\n" " </tr> \n" " <tr>\n" " <td align=\"left\" class=\"headline-two text-dark-gray\">You sent {{EmailCount}} Email<p></td>\n" " </tr>\n" " <tr>\n" " <td><img src='{{SmsSentDailyCharUrl}}' /></td>\n" " </tr>\n" " <tr>\n" " <td class=\"spacer-sm\"></td>\n" " </tr>\n" " <tr>\n" " <td align=\"left\" class=\"headline-two text-dark-gray\">Your delivery rate: {{deliveryRate}}</td>\n" " </tr>\n" " <tr>\n" " <td><img src='{{deliveryRateCharUrl}}' /></td>\n" " </tr>\n" " <tr>\n" " <td class=\"spacer-sm\"></td>\n" " </tr>\n" " <tr>\n" " <td class=\"spacer-md\"></td>\n" " </tr> \n" " <tr>\n" " <td class=\"spacer-md\"></td>\n" " </tr> \n" " <!-- Start Text -->\n" " <tr>\n" " <td class=\"spacer-sm\"></td>\n" " </tr> \n" " <tr>\n" " <td align=\"left\" class=\"body text-dark-gray\">If you have any questions or comments please contact technical support at&nbsp;<a class=\"body text-primary\" href=\"tel:1-866-848-8282\">1-866-848-8282</a>.</td>\n" " </tr>\n" " <!-- End Text -->\n" " <tr>\n" " <td class=\"spacer-lg\"></td>\n" " </tr>\n" " <!-- Start Text -->\n" " <tr>\n" " <td align=\"left\" class=\"body text-dark-gray\">GEM-CAR,<br />\n" " Automated system</td>\n" " </tr>\n" " <tr>\n" " <td class=\"spacer-md\"></td>\n" " </tr> \n" " <tr>\n" " <td align=\"left\" class=\"body text-dark-gray\">\n" " This message was sent from an unmonitored address. Please do not respond to this message.\n" " </td>\n" " </tr>\n" " <!-- End Text -->\n" " </tbody>\n" " </table>\n" " </td>\n" " </tr>\n" " <tr>\n" " <td class=\"spacer-lg\"></td>\n" " </tr>\n" " </tbody>\n" " </table>\n" " <!-- End Panel Container --></td>\n" " </tr>\n" " <tr>\n" " <td class=\"spacer-lg\"></td>\n" " </tr>\n" " <!-- Start Footer -->\n" " <tr>\n" " <td align=\"left\" id=\"footer\">\n" " <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" role=\"presentation\">\n" " <tbody>\n" " <tr>\n" " <td align=\"left\">\n" " <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" role=\"presentation\">\n" " <tbody>\n" " <tr>\n" " <td valign=\"top\" width=\"28\"><a href=\"https://www.facebook.com/GEMCARSOFTWARE\"><img alt=\"Icon\" border=\"0\" src=\"https://data.gem-support.com/s/9N2ZwefogzzJCEP/preview\" width=\"32\" /> </a></td>\n" " <td width=\"16\"></td>\n" " <td valign=\"top\" width=\"28\"><a href=\"https://twitter.com/GEMCARSOFTWARE\"><img alt=\"Icon\" border=\"0\" src=\"https://data.gem-support.com/s/4RX8koXXJp5EPHq/preview\" width=\"32\" /> </a></td>\n" " <td width=\"16\"></td>\n" " <td valign=\"top\" width=\"28\"><a href=\"https://ca.linkedin.com/pub/mathieu-brunel/3/678/466/\"><img alt=\"Icon\" border=\"0\" src=\"https://data.gem-support.com/s/zgScwGzsnTojya6/preview\" width=\"32\" /> </a></td>\n" " <td width=\"16\"></td>\n" " <td valign=\"top\" width=\"28\"><a href=\"https://www.youtube.com/user/RepairShopSoftware/\"><img alt=\"Icon\" border=\"0\" src=\"https://data.gem-support.com/s/66SSwNQy2RKYmt8/preview\" width=\"32\" /> </a></td>\n" " </tr>\n" " </tbody>\n" " </table>\n" " </td>\n" " </tr>\n" " <tr>\n" " <td class=\"spacer-sm\"></td>\n" " </tr>\n" " <tr>\n" " <td align=\"left\" class=\"body text-secondary\">&bull; V2V Technologies Inc. All rights reserved.<br />\n" " <span class=\"address\">4460 rue B&eacute;langer, Montr&eacute;al, Qc H1T 1B5</span></td>\n" " </tr>\n" " <tr>\n" " <td class=\"spacer-md\"></td>\n" " </tr>\n" " <tr>\n" " <td align=\"left\" class=\"body text-secondary\">You are receiving this email because you are a GEM-CAR customer.<br />\n" " <a class=\"body text-primary\" href=\"https://www.gem-car.com\">Visite us</a> | <a class=\"body text-primary\" href=\"https://www.gem-car.com/faqs/\">Support FAQ</a> | <a class=\"body text-primary\" href=\"{{unsubscribeURL}}\">Unsubscribe</a></td>\n" " </tr>\n" " </tbody>\n" " </table>\n" " </td>\n" " </tr>\n" " <!-- End Footer -->\n" " <tr>\n" " <td class=\"spacer-lg\"></td>\n" " </tr>\n" " </tbody>\n" "</table>\n" "<!-- End Email Container --></center>\n" "</body>\n" "</html>") matches = re.finditer(regex, test_str) 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