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

/
/
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"<tr[^>]*>[^<]*<td[^>]*>(?<No>[^<]*)<[^>]*>[^>]*>(?<ID_dist>[^<]*)<\/td>[^>]*>(?<Jmeno>[^<]*)<\/td>[^>]*>(?<Tel_Home>[^<]*)<\/td>[^>]*>(?<Tel_Buss>[^<]*)<\/td>[^>]*>(?<Tel_mob>[^<]*)<\/td>[^>]*>(?<Country>[^<]*)<\/td>[^>]*><a[^>]*>(?<Email>[^<]*)[^<]*<\/a>[^<]*<\/td>[^>]*>(?<Levels>[^<]*)<\/td>[^>]*>(?<Pozice>[^<]*)<\/td>[^>]*>(?<Sponzor_ID>[^<]*)<\/td>" 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 xmlns=\"http://www.w3.org/1999/xhtml\">\n" "<head>\n" "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n" "<title>PBC - Distributors to be Renewed List</title>\n" "<meta name=\"description\" content=\"Lifestyles PBC is an on-line tool to help our Distributors manage their business and achieve their goals.\" />\n" "<meta name=\"keywords\" content=\"Lifestyles, Lifestyles International, Personal Business Center, PBC\"/>\n" "<link href=\"css/en.css\" rel=\"stylesheet\" type=\"text/css\" />\n" "</head>\n" "<body bgcolor=\"#EAF4D1\" leftmargin=\"0\" topmargin=\"3\" marginwidth=\"0\" marginheight=\"0\">\n" "<table border=\"0\" align=\"center\" cellpadding=\"1\" cellspacing=\"1\" bgcolor=\"#999999\">\n" " <tr> <td height=\"33\" colspan=\"11\" align=\"center\" bgcolor=\"#FFFFFF\"><h2 style=\"margin: 10px\">Distributors to be Renewed List</h2>\n" " <p style=\"margin: 10px\">Month: Nov. 2017</p></td>\n" " </tr>\n" " <tr>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#CCCCCC\"><strong>No.</strong></td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#CCCCCC\"><strong>ID</strong></td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#CCCCCC\"><strong>Name</strong></td>\n" " <td align=\"center\" bgcolor=\"#CCCCCC\"><strong>Home Phone</strong></td>\n" " <td align=\"center\" bgcolor=\"#CCCCCC\"><strong>Business Phone</strong></td>\n" " <td align=\"center\" bgcolor=\"#CCCCCC\"><strong>Cell Phone</strong></td>\n" " <td align=\"center\" bgcolor=\"#CCCCCC\"><strong>Country</strong></td>\n" " <td align=\"center\" bgcolor=\"#CCCCCC\"><strong>Email</strong></td>\n" " <td align=\"center\" bgcolor=\"#CCCCCC\"><strong>Levels</strong></td>\n" " <td align=\"center\" bgcolor=\"#CCCCCC\"><strong>Rank</strong></td>\n" " <td align=\"center\" bgcolor=\"#CCCCCC\"><strong>Sponsor</strong></td>\n" " </tr>\n" " <tr>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">1</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">98000204732</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">Pavel Kaleta</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">420 773512280</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Czech Republic</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"><a href=\"mailto:\"></a></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">19</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Distributor</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">80011630542</td>\n" " </tr>\n" " <tr>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">2</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">98000229140</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">Ivana Bilkova</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">420 605934970</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Czech Republic</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"><a href=\"mailto:ifcabilkova@seznam.cz\">ifcabilkova@seznam.cz</a></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">20</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Distributor</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">98000142059</td>\n" " </tr>\n" " <tr>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">3</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">98000229289</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">Martina Vitkovska</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">420 606629784</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Czech Republic</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"><a href=\"mailto:martinavitkovska@seznam.cz\">martinavitkovska@seznam.cz</a></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">18</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Distributor</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">13980242</td>\n" " </tr>\n" " <tr>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">4</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">98000253035</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">Lenka Lovetinska</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">420 60598685</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">420 567223836</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Czech Republic</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"><a href=\"mailto:lovetinskal@seznam.cz\">lovetinskal@seznam.cz</a></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">20</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Distributor</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">98000142059</td>\n" " </tr>\n" " <tr>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">5</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">98000253463</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">Filip Korda</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">421 902483141</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">421 948186144</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Slovak Republic</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"><a href=\"mailto:korda@sanusvita.sk\">korda@sanusvita.sk</a></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">20</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Distributor</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">80011633242</td>\n" " </tr>\n" " <tr>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">6</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">CZ-03-04277</td>\n" " <td height=\"27\" align=\"center\" bgcolor=\"#FFFFFF\">Olga Kostomarova</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">420 603160599</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Czech Republic</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\"><a href=\"mailto:\"></a></td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">19</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">Distributor</td>\n" " <td align=\"center\" bgcolor=\"#FFFFFF\">80011630542</td>\n" " </tr>\n" " </table>\n" "</body>\n" "</html>\n" "<script>window.focus()</script>\n") 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