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

r"
"
s

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"<script>.*?(var.*?;).*?</script>" test_str = ("<script><!--\n" "var STATIC_BASE = '//cdn1.editmysite.com/';\n" "var STYLE_PREFIX = 'wsite';\n" "//-->\n" "</script>\n" "<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>\n" "<script src='//cdn2.editmysite.com/js/site/main.js?buildTime=1400799662'></script>\n" "<script>_W.relinquish && _W.relinquish()</script>\n" "<script type='text/javascript'><!--\n" "(function(jQuery){\n" "function initFlyouts(){initPublishedFlyoutMenus([{\"id\":\"408147401478827578\",\"title\":\"HOME\",\"url\":\"index.html\",\"target\":\"\"},{\"id\":\"852985120278826572\",\"title\":\"GET A DOMAIN\",\"url\":\"get-a-domain.html\",\"target\":\"\"},{\"id\":\"745935904556716097\",\"title\":\"SERVICES\",\"url\":\"services.html\",\"target\":\"\"},{\"id\":\"505441827833699923\",\"title\":\"MY ACCOUNT\",\"url\":\"my-account.html\",\"target\":\"\"},{\"id\":\"464549931862712377\",\"title\":\"ABOUT US\",\"url\":\"about-us.html\",\"target\":\"\"}],\"408147401478827578\",\"<li><a href='#'>{{title}}<\\/a><\\/li>\",'active',false)}\n" "if (jQuery) {\n" "if (jQuery.browser.msie && !window.flyoutMenusRefreshable) window.onload = initFlyouts;\n" "else jQuery(initFlyouts);\n" "}else{\n" "if (Prototype.Browser.IE) window.onload = initFlyouts;\n" "else document.observe('dom:loaded', initFlyouts);\n" "}\n" "})(window._W && _W.jQuery)\n" "//-->\n" "</script>\n" "</head>\n" "<body class=' wsite-theme-light wsite-page-index'>\n" "<div id=\"fb-root\"></div>\n" "<script>(function(d, s, id) {\n" " var js, fjs = d.getElementsByTagName(s)[0];\n" " if (d.getElementById(id)) return;\n" " js = d.createElement(s); js.id = id;\n" " js.src = \"//connect.facebook.net/en_US/sdk.js#xfbml=1&appId=109188665774734&version=v2.0\";\n" " fjs.parentNode.insertBefore(js, fjs);\n" "}(document, 'script', 'facebook-jssdk'));</script>\n" "<div id=\"nav-fixed\">\n" " <div id=\"nav-body\">\n" " <span style=\"visibility:hidden\">Pwns.me</span> <ul class='wsite-menu-default'><li id='active'><a href='/'>HOME</a></li><li id='pg852985120278826572'><a href='/get-a-domain.html'>GET A DOMAIN</a></li><li id='pg745935904556716097'><a href='/services.html'>SERVICES</a></li><li id='pg505441827833699923'><a href='/my-account.html'>MY ACCOUNT</a></li><li id='pg464549931862712377'><a href='/about-us.html'>ABOUT US</a></li></ul>\n" " </div>\n" "</div>\n" "<div id=\"pwns-logo\">\n" "<span style=\"display:hidden;\" david=\"gay\"></span>\n" "</div>\n" "<div id=\"content-body\">\n" "<div id=\"titleImg\">\n" "<img src=\"/files/theme/middlef.png\">\n" "</div>\n" "<div id=\"que\">\n" "<ul>\n" "<li><a href=\"about-us.html\">QUE?</a> </li>\n" "<li><a href=\"get-a-domain.html\">SEEMS LEGIT</a> </li>\n" "</ul>\n" "</div>\n\n" "</div>\n\n" "<div id=\"social\">\n" "<div class=\"fb-like\" data-href=\"http://pwns.me\" data-layout=\"button\" data-action=\"like\" data-show-faces=\"false\" data-share=\"false\"></div><br><br>\n" "<a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-url=\"http://pwns.me\" data-text=\"Pwns.me - Awesome domains\" data-count=\"none\" data-hashtags=\"pwns\">Tweet</a>\n" "<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>\n" "</div>\n\n" "<div id=\"myFooter\"> Brought to you by Awesomness. </div>\n" "<div style=\"display:none;\">\n" "<div><div class='wsite-elements wsite-footer'>\n" "<div><div class=\"wsite-multicol\"><div class='wsite-multicol-table-wrap' style='margin:0 -15px'>\n" "<table class='wsite-multicol-table'>\n" "<tbody class='wsite-multicol-tbody'>\n" "<tr class='wsite-multicol-tr'>\n" "<td class='wsite-multicol-col' style='width:50%;padding:0 15px'></td>\n" "<td class='wsite-multicol-col' style='width:50%;padding:0 15px'></td>\n" "</tr>\n" "</tbody>\n" "</table>\n" "</div></div></div>\n\n" "<div class=\"weebly-footer\" style=\"text-align:right;\"><div style=\"height:10px;overflow:hidden\"></div>\n" "Proudly powered by <a href=\"http://www.weebly.com/?utm_source=internal&utm_medium=footer&utm_campaign=2\" href=\"_blank\">Weebly</a>\n" "<div style=\"height:10px;overflow:hidden\"></div></div></div>\n" "<script type='text/javascript'>\n" "<!--\n\n" "if (document.cookie.match(/(^|;)\\s*is_mobile=1/)) {\n" " var windowHref = window.location.href || '';\n" " if (windowHref.indexOf('?') > -1) {\n" " windowHref += '&';\n" " } else {\n" " windowHref += '?';\n" " }\n" " document.write(\n" " \"&nbsp;&nbsp;&nbsp;&nbsp;\" +\n" " \"<a class='wsite-view-link-mobile' href='\" + windowHref + \"view=mobile'>Mobile Site</a>\"\n" " );\n" "}\n") matches = re.search(regex, test_str, re.DOTALL) if matches: print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group())) for groupNum in range(0, len(matches.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.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