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"
"
ms

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"<td width=\"70%\" class=\"tdbold\">\s+<div >\s*([^<&]+).*ที่อยู่\s+([^<&]+).*โทร.\s*([0-9๐-๙- ,]+)" test_str = ("\n" "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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" "<script type=\"text/javascript\">\n" " var _gaq = _gaq || [];\n" " _gaq.push(['_setAccount', 'UA-18733385-1']);\n" " _gaq.push(['_trackPageview']);\n" " (function() {\n" " var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n" " ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n" " var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n" " })();\n" "</script>\n" " <title>ระบบสารสนเทศภฺมิศาสตร์ทรัพยากรสุขภาพ สำนักงานสาธารณสุขจังหวัดสมุทรปราการ</title>\n" " <script type=\"text/javascript\" src=\"jquery/jquery-1.3.2.min.js\"></script> \n" " <script type=\"text/javascript\" src=\"jquery/jquery.easing.1.3.js\"></script> \n" " <script type=\"text/javascript\" src=\"jquery/jquery.galleryview-1.1.js\"></script> \n" " <script type=\"text/javascript\" src=\"jquery/jquery.timers-1.1.2.js\"></script> \n" " <script type=\"text/javascript\" src=\"js/jquery_slide.js\"></script> \n" " <link href=\"css/stylemap.css\" rel=\"stylesheet\" type=\"text/css\">\n" " <script type=\"text/javascript\" src=\"../ckeditor/ckeditor.js\"></script>\n" " <script type=\"text/javascript\">\n" " //<![CDATA[\n" " CKEDITOR.replace( 'message',{\n" " height : 400,\n" " width : 400,\n" " } );\n" " //]]>\n" " </script>\n" " <script type=\"text/javascript\" src=\"highslide/highslide-with-gallery.js\"></script>\n" "<link rel=\"stylesheet\" type=\"text/css\" href=\"highslide/highslide.css\" />\n\n" " <script type=\"text/javascript\">\n" " hs.graphicsDir = 'highslide/graphics/';\n" " hs.align = 'center';\n" " hs.transitions = ['expand', 'crossfade'];\n" " hs.outlineType = 'glossy-dark';\n" " hs.wrapperClassName = 'dark';\n" " hs.fadeInOut = true;\n\n" " if (hs.addSlideshow) hs.addSlideshow({\n" " interval: 4000,\n" " repeat: false,\n" " useControls: true,\n" " fixedControls: 'fit',\n" " overlayOptions: {\n" " opacity: .6,\n" " position: 'bottom center',\n" " hideOnMouseOut: true}\n" " });\n\n" "</script>\n" " </head>\n" "<body>\n\n" "<table align =\"center\" width=\"760\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>\n" "<div id=\"navcontainer\">\n" " <ul id=\"navlist\">\n" " <!-- CSS Tabs -->\n" " \n" "<li><a href=\"info.php?maincode=00001\" id=\"current\">ข้อมูลทั่วไป</a></li>\n" "<li><a href=\"infopersonal.php?maincode=00001\">ข้อมูลบุคลากร</a></li>\n" "<li><a href=\"infospecialty.php?maincode=00001\">แพทย์เฉพาะทาง</a></li>\n" "<li><a href=\"infoequip.php?maincode=00001\">สถานที่และเครื่องมือแพทย์</a></li>\n" "<li><a href=\"work.php?maincode=00001\">ผลงานเด่น</a></li>\n" "<li><a href=\"login.php?maincode=00001\">เข้าสู่ระบบ</a></li>\n\n" " </ul>\n" " </div>\n" " \n" "</td></tr></table>\n" "<!--table width=\"761\" align=\"center\" class=\"tableBorder\">\n" "<tr>\n" "<td width=\"753\"-->\n" "<table width=\"650\" align=\"center\" bgcolor=\"#FFFFFF\" cellpadding=\"0\" cellspacing=\"0\">\n" " <tr>\n" " <td colspan=\"2\" class=\"tdhead650\" valign=\"middle\">ข้อมูลทั่วไป สำนักงานสาธารณสุขจังหวัดสมุทรปราการ</td>\n" " </tr>\n" " <tr>\n" " <td width=\"30%\">&nbsp;</td>\n" " <td width=\"70%\">&nbsp;</td>\n" " </tr>\n" " <tr>\n" " <td align=\"center\" valign=\"top\"> <div class=\"highslide-gallery\"> <a href=\"upload/logo/p_00001_190418_031102.png\" class=\"highslide\" onclick=\"return hs.expand(this)\">\n" "<img src=\"upload/logo/p_00001_190418_031102.png\" alt=\"Highslide JS\"title=\"Click to enlarge\" width=\"100\" height=\"100\"/></a>\n" "<div class=\"highslide-caption\"><center> สำนักงานสาธารณสุขจังหวัดสมุทรปราการ</center></div></td>\n" " <td width=\"70%\" class=\"tdbold\">\n" " <div > สำนักงานสาธารณสุขจังหวัดสมุทรปราการ&nbsp;&nbsp;<a href=\"gmap.php?code=00001\" target=\"_blank\"><img src=\"image_res/2green.png\" title=\"แสดงในแผนที่\" border=\"0\"/></a></div><br />\n" " <div> รหัส 00001</div><br />\n" " <div>ที่อยู่ 19 ซอย 35 อัศวนนท์ 2 ถ.สุขุมวิท ต.ปากน้ำ อ.เมือง จ.สมุทรปราการ 10270 </div><br />\n" " <div>ที่ตั้ง จ.สมุทรปราการ อ.เมืองสมุทรปราการ ต.ปากน้ำ หมู่ 0 </div> \n" " <br>\n\n\n" " \n" " <div>โทร. 02-3895980&nbsp;&nbsp; แฟกซ์ 02-3951034 </div>\n" "<br> \n\n" " \n" " <div>อีเมล์ -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; เว็บไซต์ <a href=\"http://www.spko.moph.go.th/\" target=\"_new\" >www.spko.moph.go.th/</a></div></td>\n" " </tr>\n" " <tr>\n" " <td>&nbsp;</td>\n" " <td></td>\n" " </tr>\n" " <tr>\n" " <td>&nbsp;</td>\n" " <td class=\"trBottom\">ผู้บริหารและประวัติสถานพยาบาล</td>\n" " </tr>\n" " <tr>\n" " <td align=\"center\"> <div class=\"highslide-gallery\"> <a href=\"upload/ceo/p_00001_140218_134154.jpg\" class=\"highslide\" onclick=\"return hs.expand(this)\">\n" "<img src=\"upload/ceo/p_00001_140218_134154.jpg\" alt=\"Highslide JS\"title=\"Click to enlarge\" width=\"100\" height=\"100\"/></a>\n" "<div class=\"highslide-caption\"><center> สำนักงานสาธารณสุขจังหวัดสมุทรปราการ</center></div></td>\n" " <td><br ><br />\n" " <div >ชื่อผู้บริหาร <b>&nbsp;นพ.สวัสดิ์ อภิวัจนีวงศ์</b></div>\n" " <br />\n" " <div >\n" " ประวัติสถานพยาบาล\n" "<a href=\"info_history.php?maincode=00001\" title=\"อ่านประวัติสถานพยาบาล\"><b>เพิ่มเติม</b></a> <p>&nbsp;</p>\n\n" " \n" " </div>\n" " </td>\n" " </tr>\n" " <tr>\n" " <td>&nbsp;</td>\n" " <td></td>\n" " </tr>\n" " \n" " \n" "</table>\n" "<table align=\"center\">\n" "<tr>\n" "<td>\n" "<body><div class=\"highslide-gallery\">\n\n" " \n" " <a href=\"upload/hospital/p_00001_190418_024043.jpg\" class=\"highslide\" onClick=\"return hs.expand(this)\">\n" "<img src=\"upload/hospital/p_00001_190418_024043.jpg\" alt=\"Highslide JS\"title=\"Click to enlarge\" width=\"133\" height=\"100\"/></a>\n" "<div class=\"highslide-caption\"><center> สำนักงานสาธารณสุขจังหวัดสมุทรปราการ</center></div>\n" " \n" " <a href=\"upload/hospital/p_00001_190418_024156.jpg\" class=\"highslide\" onClick=\"return hs.expand(this)\">\n" "<img src=\"upload/hospital/p_00001_190418_024156.jpg\" alt=\"Highslide JS\"title=\"Click to enlarge\" width=\"133\" height=\"100\"/></a>\n" "<div class=\"highslide-caption\"><center> สำนักงานสาธารณสุขจังหวัดสมุทรปราการ</center></div>\n" " \n" " <a href=\"upload/hospital/p_00001_190418_024306.jpg\" class=\"highslide\" onClick=\"return hs.expand(this)\">\n" "<img src=\"upload/hospital/p_00001_190418_024306.jpg\" alt=\"Highslide JS\"title=\"Click to enlarge\" width=\"133\" height=\"100\"/></a>\n" "<div class=\"highslide-caption\"><center> สำนักงานสาธารณสุขจังหวัดสมุทรปราการ</center></div>\n" " \n" " <a href=\"upload/hospital/p_00001_190418_025428.jpg\" class=\"highslide\" onClick=\"return hs.expand(this)\">\n" "<img src=\"upload/hospital/p_00001_190418_025428.jpg\" alt=\"Highslide JS\"title=\"Click to enlarge\" width=\"133\" height=\"100\"/></a>\n" "<div class=\"highslide-caption\"><center> สำนักงานสาธารณสุขจังหวัดสมุทรปราการ</center></div>\n" " <br>\n\n\n" "</div>\n\n\n\n" "</div>\n" "</body>\n" "</td>\n" "</tr>\n" "</table>\n\n" "<table width=\"650\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">\n" "<tr>\n" " <td align=\"right\" class=\"trBottom\">&nbsp;</td>\n" " </tr>\n" " <tr>\n" " <td align=\"right\" >ข้อมูลวันที่ 6 ธ.ค. 2562 15:51</td>\n" " </tr>\n" "</table>\n" "<!--/td></tr></table-->\n" "<TABLE border ='0' width =\"650\" align=\"center\">\n" "<tr><td>&nbsp;</td></tr>\n" "<TR><TD width =100% align= \"center\"><a href=\"https://play.google.com/store/apps/details?id=com.hiso.hisomap4\" target=\"_blank\"><img src=\"app_android.png\" alt=\"ดาวน์โหลดได้ที่ Google Play\"/></a><!-- Begin ShinyStat Free code -->\n" "<script type=\"text/javascript\" language=\"JavaScript\" src=\"http://codice.shinystat.com/cgi-bin/getcod.cgi?USER=healthHiso\"></script>\n" "<noscript>\n" "<a href=\"http://www.shinystat.com\" target=\"_top\">\n" "<img src=\"http://www.shinystat.com/cgi-bin/shinystat.cgi?USER=healthHiso\" alt=\"Free hit counter\" border=\"0\" /></a>\n" "</noscript><a href=\"https://itunes.apple.com/th/app/gis-health/id859027531?mt=8\" target=\"_blank\"><img src=\"app_ios.png\" alt=\"ดาวน์โหลดได้ที่ App Store\"/></a>\n" "</TD><TD></TD></TR>\n" "</TABLE>\n" "</body>\n" "</html>") matches = re.search(regex, test_str, re.MULTILINE | 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