Regular Expressions 101

Save & Share

  • Regex Version: ver. 3
  • 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

/
/
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"getObj\(\"Frm_Logintoken\"\).value = \"(.*)\";" test_str = ("\n" "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\">\n" "<head>\n" "<META HTTP-EQUIV=\"pragma\" CONTENT=\"no-cache\">\n" "<META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache, must-revalidate\">\n" "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n" "<title>&#90;&#88;&#72;&#78;&#32;&#72;&#49;&#48;&#56;&#78;&#32;&#86;&#50;&#46;&#53;</title>\n" "<LINK REL=\"stylesheet\" HREF=\"css/login.css\" TYPE=\"text/css\" />\n" "<LINK REL=\"stylesheet\" HREF=\"css/styleen.css\" TYPE=\"text/css\" />\n" "<script>\n\n" "function getObj(id)\n" "{\n" "return(document.getElementById(id));\n" "}\n" "var SetTFlag = 0;\n" "var maxtime;\n" "var interval;\n" "function setTime()\n" "{\n" "if(window.name>=60 || window.name<0 || window.name==\"\")\n" "{\n" "maxtime = Math.min(60, 0 + 60 - 218143);\n" "}\n" "else\n" "{\n" "maxtime = window.name;\n" "}\n" "if(maxtime>=0)\n" "{\n" "seconds = maxtime;\n" "getObj(\"time\").innerHTML=seconds+\" secs\";\n" "if (maxtime == 0)\n" "{\n" "setDisable();\n" "SetTFlag =0;\n" "window.name=\"\";\n" "if(interval)\n" "{\n" "clearInterval(interval);\n" "interval = 0;\n" "}\n" "}\n" "else\n" "{\n" "--maxtime;\n" "SetTFlag = 1;\n" "window.name = maxtime;\n" "}\n" "}\n" "}\n" "function setinner(type)\n" "{\n" "if (SetTFlag ==0)\n" "{\n" "getObj(\"myLayer\").style.visibility = type ;\n" "}\n" "}\n" "function Transfer_meaning(id,value)\n" "{\n" "getObj(id).value=value;\n" "}\n" "function ChangeLang()\n" "{\n" "var lang = \"English\";\n" "getObj(\"_lang\").disabled = false;\n" "getObj(\"Frm_Username\").disabled = true;\n" "getObj(\"Frm_Password\").disabled = true;\n" "if(true != getObj(\"TestLang01\").disabled)\n" "{\n" "if (\"English\" == lang)\n" "{\n" "getObj(\"_lang\").value = \"Chinese\";\n" "}\n" "else if (\"Chinese\" == lang)\n" "{\n" "getObj(\"_lang\").value = \"English\";\n" "}\n" "getObj(\"action\").value = \"setlang\";\n" "getObj(\"Frm_Username\").disabled = true;\n" "getObj(\"Frm_Password\").disabled = true;\n" "getObj(\"LoginId\").disabled = true;\n" "document.fLogin.submit();\n" "}\n" "}\n" "</script>\n" "</head>\n\n" "<body>\n" "<div id=\"container\">\n" "<div id=\"myLayer\" onmousedown=\"setinner('hidden')\" style=\"position:absolute; width:185px; height:91px; z-index:9; right: 50px; top: 140px; background: url(img/pop_up.gif) no-repeat; border: 1px none #000000; visibility: hidden;\">\n" "<div id=\"Layer1\" style=\"position:absolute; width:180px; height:45px; z-index:10; left: 2px; top: 28px;\">\n" "<table width=\"183\" border=\"0\">\n" "<tr>\n" "<td width=\"183\">&nbsp;&nbsp;<font id=\"errmsg\" class=\"notecontent\" ></font><span id=\"time\" style=\"color:red;display:none;\"></span></td>\n" "</tr>\n" "</table>\n" "</div>\n" "<div id=\"Layer3\" style=\"position:absolute; width:140px; height:10px; z-index:11; left: 34px; top: 9px; font-size: 8pt;\">\n" "<font class=\"note\" id=\"errnote\">&nbsp;Error</font>\n" "</div>\n" "</div>\n" "<!--head²¿·Ö¿ªÊ¼-->\n" "<div id=\"head\">\n" "<div class=\"type\"><font id=\"\">&#90;&#88;&#72;&#78;&#32;&#72;&#49;&#48;&#56;&#78;&#32;&#86;&#50;&#46;&#53;</font></div>\n" "<div id=\"banner\" style=\"background-image:url(img/banner.gif); background-repeat:no-repeat\"></div>\n" "</div>\n" "<!--head²¿·Ö½áÊø-->\n" "<!--content²¿·Ö¿ªÊ¼-->\n" "<div id=\"loginArea\">\n" "<form name=\"fLogin\" id=\"fLogin\" method=\"post\" onsubmit=\"return false;\" action=\"\">\n" "<input type=\"hidden\" name=\"_lang\" id=\"_lang\" value=\"\" disabled>\n" "<input type=\"hidden\" name=\"frashnum\" id=\"frashnum\" value=\"\">\n" "<input type=\"hidden\" name=\"action\" id=\"action\" value=\"login\">\n" "<input type=\"hidden\" name=\"Frm_Logintoken\" id=\"Frm_Logintoken\" value=\"\">\n" "<!--ÄÚÈÝ¿ªÊ¼-->\n" "<div class=\"login_frame\">\n" "<ul class=\"login_title\">\n" "<li class=\"login_title_left\"></li>\n" "<li class=\"login_title_center\">Please login to continue...</li>\n\n" "<li class=\"login_title_right\"></li>\n" "</ul>\n" "<div class=\"content login_content\">\n" "<ul class=\"login_blank\"></ul>\n" "<ul class=\"login_ul_1\">\n" "<li class=\"login_li_1\"><span>Username</span></li>\n" "<li class=\"login_li_2\">\n" "<input type=\"text\" class=\"username\" name=\"Username\" id=\"Frm_Username\">\n" "</li>\n" "</ul>\n" "<ul class=\"login_blank\"></ul>\n" "<ul class=\"login_ul_1\">\n" "<li class=\"login_li_1\"><span>Password</span></li>\n" "<li class=\"login_li_2\">\n" "<input type=\"password\" class=\"password\" name=\"Password\" id=\"Frm_Password\">\n" "</li>\n" "<li class=\"login_li_3\">\n" "<input class=\"login\" type=\"submit\" id=\"LoginId\" value=\"Login\" onclick=\"dosubmit()\">\n" "</li>\n" "</ul>\n" "</div>\n" "</div>\n" "<!--ÄÚÈݽáÊø-->\n" "</form>\n" "</div>\n" "<!--bottom²¿·Ö¿ªÊ¼-->\n\n" "<div style=\"font-family: Arial;font-size: 12px;text-align:center;\"> &copy; 2008-2015 ZTE Corporation. All rights reserved </div>\n\n" "<div class=\"bottom_line\"></div>\n" "<div id=\"bottomx\"></div>\n" "<!--bottom²¿·Ö½áÊø-->\n" "</div>\n" "</body>\n" "</html>\n" "<script>\n\n" "window.name=\"\";\n\n" "function SetDisabled()\n" "{\n" "getObj(\"errmsg\").innerHTML = \"You have input the wrong username or password for three times. Please try again a minute later.\";\n" "getObj(\"Frm_Username\").disabled = true;\n" "getObj(\"Frm_Password\").disabled = true;\n" "getObj(\"LoginId\").disabled = true;\n\n" "getObj(\"myLayer\").style.visibility = \"visible\" ;\n" "getObj(\"time\").style.display = \"\" ;\n" "interval = setInterval(\"setTime()\",1000);\n" "}\n" "function setDisable()\n" "{\n" "if (SetTFlag == 1)\n" "{\n" "getObj(\"Frm_Username\").disabled = false;\n" "getObj(\"Frm_Password\").disabled = false;\n" "getObj(\"LoginId\").disabled = false;\n\n" "getObj(\"time\").style.display = \"none\";\n" "getObj(\"myLayer\").style.visibility = \"hidden\";\n" "}\n" "}\n" "function dosubmit()\n" "{\n" "if (getObj(\"Frm_Username\").value == \"\")\n" "{\n" "getObj(\"errmsg\").innerHTML = \"Username cannot be empty.\";\n" "getObj(\"myLayer\").style.visibility = \"visible\" ;\n" "return;\n" "}\n" "else\n" "{\n" "getObj(\"LoginId\").disabled = true;\n" "getObj(\"Frm_Logintoken\").value = \"7\";\n" "document.fLogin.submit();\n" "}\n" "}\n" "</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