Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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

r"
"
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"(?<=<h3>).*?(?=</h3>)" test_str = ("<!DOCTYPE HTML>\n" "<html>\n\n" "<head>\n" " <title>black_pink_white - examples</title>\n" " <meta name=\"description\" content=\"website description\" />\n" " <meta name=\"keywords\" content=\"website keywords, website keywords\" />\n" " <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />\n" " <link rel=\"stylesheet\" type=\"text/css\" href=\"style/style.css\" title=\"style\" />\n" "</head>\n\n" "<body>\n" " <div id=\"main\">\n" " <div id=\"header\">\n" " <div id=\"logo\">\n" " <div id=\"logo_text\">\n" " <!-- class=\"logo_colour\", allows you to change the colour of the text -->\n" " <h1><a href=\"index.html\">black<span class=\"logo_colour\">_pink</span><span class=\"logo_colour2\">_white</span></a></h1>\n" " <h2>Simple. Contemporary. Website Template.</h2>\n" " </div>\n" " </div>\n" " <div id=\"menubar\">\n" " <ul id=\"menu\">\n" " <!-- put class=\"selected\" in the li tag for the selected page - to highlight which page you're on -->\n" " <li><a href=\"index.html\">Home</a></li>\n" " <li class=\"selected\"><a href=\"examples.html\">Examples</a></li>\n" " <li><a href=\"page.html\">A Page</a></li>\n" " <li><a href=\"another_page.html\">Another Page</a></li>\n" " <li><a href=\"contact.html\">Contact Us</a></li>\n" " </ul>\n" " </div>\n" " </div>\n" " <div id=\"site_content\">\n" " <div class=\"sidebar\">\n" " <!-- insert your sidebar items here -->\n" " <h3>Latest News</h3>\n" " <h4>New Website Launched</h4>\n" " <h5>August 1st, 2014</h5>\n" " <p>2014 sees the redesign of our website. Take a look around and let us know what you think.<br /><a href=\"#\">Read more</a></p>\n" " <p></p>\n" " <h4>New Website Launched</h4>\n" " <h5>August 1st, 2014</h5>\n" " <p>2014 sees the redesign of our website. Take a look around and let us know what you think.<br /><a href=\"#\">Read more</a></p>\n" " <h3>Useful Links</h3>\n" " <ul>\n" " <li><a href=\"#\">link 1</a></li>\n" " <li><a href=\"#\">link 2</a></li>\n" " <li><a href=\"#\">link 3</a></li>\n" " <li><a href=\"#\">link 4</a></li>\n" " </ul>\n" " <h3>Search</h3>\n" " <form method=\"post\" action=\"#\" id=\"search_form\">\n" " <p>\n" " <input class=\"search\" type=\"text\" name=\"search_field\" value=\"Enter keywords.....\" />\n" " <input name=\"search\" type=\"image\" style=\"border: 0; margin: 0 0 -9px 5px;\" src=\"style/search.png\" alt=\"Search\" title=\"Search\" />\n" " </p>\n" " </form>\n" " </div>\n" " <div id=\"content\">\n" " <!-- insert the page content here -->\n" " <h1>Examples</h1>\n" " <p>This page contains examples of all the styled elements available as part of this design. Use this page for reference, whilst you build your website.</p>\n" " <h2>Headings</h2>\n" " <p>These are the different heading formats:</p>\n" " <h1>Heading 1</h1>\n" " <h2>Heading 2</h2>\n" " <h3>Heading 3</h3>\n" " <h4>Heading 4</h4>\n" " <h5>Heading 5</h5>\n" " <h6>Heading 6</h6>\n" " <h2>Text</h2>\n" " <p>The following examples show how the text (within '&lt;p&gt;&lt;/p&gt;' tags) will appear:</p>\n" " <p><strong>This is an example of bold text</strong></p>\n" " <p><i>This is an example of italic text</i></p>\n" " <p><a href=\"#\">This is a hyperlink</a></p>\n" " <h2>Lists</h2>\n" " <p>This is an unordered list:</p>\n" " <ul>\n" " <li>Item 1</li>\n" " <li>Item 2</li>\n" " <li>Item 3</li>\n" " <li>Item 4</li>\n" " </ul>\n" " <p>This is an ordered list:</p>\n" " <ol>\n" " <li>Item 1</li>\n" " <li>Item 2</li>\n" " <li>Item 3</li>\n" " <li>Item 4</li>\n" " </ol>\n" " <h2>Images</h2>\n" " <p>images can be placed on the left, in the center or on the right:</p>\n" " <span class=\"left\"><img src=\"style/graphic.png\" alt=\"example graphic\" /></span>\n" " <p>\n" " Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor\n" " incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud\n" " exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute\n" " irure dolor in reprehenderit in voluptate velit esse cillum.\n" " </p>\n" " <span class=\"center\"><img src=\"style/graphic.png\" alt=\"example graphic\" /></span>\n" " <span class=\"right\"><img src=\"style/graphic.png\" alt=\"example graphic\" /></span>\n" " <p>\n" " Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor\n" " incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud\n" " exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute\n" " irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla\n" " pariatur.\n" " </p>\n" " <h2>Tables</h2>\n" " <p>Tables should be used to display data and not used for laying out your website:</p>\n" " <table style=\"width:100%; border-spacing:0;\">\n" " <tr><th>Item</th><th>Description</th></tr>\n" " <tr><td>Item 1</td><td>Description of Item 1</td></tr>\n" " <tr><td>Item 2</td><td>Description of Item 2</td></tr>\n" " <tr><td>Item 3</td><td>Description of Item 3</td></tr>\n" " <tr><td>Item 4</td><td>Description of Item 4</td></tr>\n" " </table>\n" " <h2>Form Elements</h2>\n" " <form action=\"#\" method=\"post\">\n" " <div class=\"form_settings\">\n" " <p><span>Form field example</span><input type=\"text\" name=\"name\" value=\"\" /></p>\n" " <p><span>Textarea example</span><textarea rows=\"8\" cols=\"50\" name=\"name\"></textarea></p>\n" " <p><span>Checkbox example</span><input class=\"checkbox\" type=\"checkbox\" name=\"name\" value=\"\" /></p>\n" " <p><span>Dropdown list example</span><select id=\"id\" name=\"name\"><option value=\"1\">Example 1</option><option value=\"2\">Example 2</option></select></p>\n" " <p style=\"padding-top: 15px\"><span>&nbsp;</span><input class=\"submit\" type=\"submit\" name=\"name\" value=\"button\" /></p>\n" " </div>\n" " </form>\n" " </div>\n" " </div>\n" " <div id=\"footer\">\n" " Copyright &copy; black_pink_white | <a href=\"http://validator.w3.org/check?uri=referer\">HTML5</a> | <a href=\"http://jigsaw.w3.org/css-validator/check/referer\">CSS</a> | free from html5webtemplates.co.uk\n" " </div>\n" " </div>\n" "</body>\n" "</html>\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