{"id":89,"date":"2024-02-15T21:18:25","date_gmt":"2024-02-15T20:18:25","guid":{"rendered":"https:\/\/pythonium.net\/?page_id=89"},"modified":"2024-02-15T21:10:05","modified_gmt":"2024-02-15T19:10:05","slug":"regex","status":"publish","type":"page","link":"https:\/\/pythonium.net\/regex","title":{"rendered":"Python Regex Tester"},"content":{"rendered":"\n<style>\n\t.menu-lang{float:none;margin-bottom: 20px;}\n\t.CodeMirror {\n\t\tfont-size:16px;\n\t}\n\n.middle {\ntext-align: center;\n}\n\t.table td {\n\t\ttext-align:left;\n\t}\n\t.regExMatch0 {\n\t\tbackground-color: #ADC4CC;\n\t}\n\t.regExMatch1 {\n\t\tbackground-color: #9B539C;\n\t}\n\t.explanation {\n\t\t\/* overflow-x: scroll; *\/\n\t\talign:center;\n\t\ttext-align:center;\n\t}\n\t.collapsible {\n\t  background-color: #eee;\n\t  color: black;\n\t  cursor: pointer;\n\t  padding: 11px;\n\t  width: 100%;\n\t  box-sizing: border-box;\n\t  border: none;\n\t  text-align: left;\n\t  outline: none;\n\t  font-size: 15px;\n\t}\n\t.collapsible:after {\n\t\tcontent: '\\002B'; \n\t\tcolor: black;\n\t\tfont-weight: bold;\n\t\tfloat: right;\n\t\tmargin-left: 5px;\n\t}\n\t.collapsible-active, .collapsible:hover {\n\t  background-color: rgb(105, 105, 105);\n\t}\n\t.collapsible-content{\n\t  padding: 0 10px;\n\t  display: none;\n\t  overflow: hidden;\n\t  background-color: #f1f1f1;\n\t}\n\t.editor-container-regexp{\n\t\tdisplay:inline-block;\n\t\tborder: 2px solid #bbbbbb;\n\t\tpadding: 0px;\n\t\tmargin-top: 8px;\n\t\twidth:99%;\n\t\tvertical-align:top;\n\t}\n\t.flag{\n\t\ttext-align:center;\n\t\tdisplay:inline-block;\n\t\tborder: 2px solid #bbbbbb;\n\t\tborder-width: 2px 2px 2px 0;\n\t\tdisplay:inline-block;\n\t\twidth:13%;\n\t\tmargin-top: 8px;\n\t\tvertical-align:top;\n\t}\n\t.flag:hover{\n\tcolor: white;\n\t}\n\t.flag:hover::after{\n\tcolor: white;\n\t}\n\t.regex-quick-section {\n\t\tbackground-color:#4DAF52;\n\t\tmargin:8px auto 0px auto;\n\t\tcolor:white;\n\t\tfont-weight: bold;\n\t\ttext-align:center;\n\t\twidth:90%;\n\t\tborder-radius: 2px 2px 0px 0px;\n\t}\n\t.regex-quick-section:hover {\n\t\tbackground-color:#4DAF52;\n\t}\n\t.regex-quick-section:after {\n\t\tcolor:white;\n\t}\n\t.regex-quick-section:hover::after {\n\t\tcolor:white;\n\t}\n\t.regex-quick-content {\n\t\twidth:90%;\n\t\tmargin:auto;\n\t}\n\t.regex-quick-impair {\n\t\tbackground-color:#4DAF52;\n\t}\n\t.table {\n\t\twidth:100%;\n\t}\n\t.regex-quick-first{\n\t\twidth:20%;\n\t\tfont-weight:bold;\n\t}\n\t.regex-quick-second{\n\t\twidth:80%;\n\t}\ninput[type=\"button\"].noflag{background-color: #D3D3D3;}\n\t#engine {padding:10px; font-size: 1em; margin-left:8%; }\n\t.editor-msg{min-height:34px;}\n\t#engine label { font-size: 18px; font-weight: bold; }\n\t\n.radio {\n  margin: 0.5rem;\n}\n.editor-valid {\n    color: green;\n    padding: 8px;\n    background-color: #d0f5a9;\n    margin: 10px 0;\n}\n.editor-error {\n    color: red;\n    padding: 8px;\n    background-color: #f6cece;\n    margin: 10px 0;\n}\n.editor-valid , .editor-error  {\n    width: 99%;\n    margin-right: auto;\n    margin-left: auto;\n}\n\n.single-post .post-header {\n    margin-bottom: 0px;\n}\n.radio input[type=\"radio\"] {\n  position: absolute;\n  opacity: 0;\n}\n.radio input[type=\"radio\"] + .radio-label:before {\n  content: \"\";\n  background: #f4f4f4;\n  border-radius: 100%;\n  border: 1px solid #4DAF52;\n  display: inline-block;\n  width: 1.4em;\n  height: 1.4em;\n  position: relative;\n  top: -0.2em;\n  margin-right: 1em;\n  vertical-align: top;\n  cursor: pointer;\n  text-align: center;\n  -webkit-transition: all 250ms ease;\n  transition: all 250ms ease;\n}\n.radio input[type=\"radio\"]:checked + .radio-label:before {\n  background-color: #4DAF52;\n  box-shadow: inset 0 0 0 4px #f4f4f4;\n}\n.radio input[type=\"radio\"]:focus + .radio-label:before {\n  outline: none;\n  border-color: #4DAF52;\n}\n.radio input[type=\"radio\"]:disabled + .radio-label:before {\n  box-shadow: inset 0 0 0 4px #f4f4f4;\n  border-color: #4DAF52;\n  background: #4DAF52;\n}\n.radio input[type=\"radio\"] + .radio-label:empty:before {\n  margin-right: 0;\n}\ninput[type=\"button\"] { margin-top: 4px; } \n.regex-generate-string {\n\tfloat:right;\n\tmargin-right:3px;\n\tfont-weight:bold;\n}\n.editor-container { border: 1px solid black; margin-top: 5px;} \n.regex-space-top { margin-top:3px; }\n.regex-pointer { cursor: pointer; }\n.nolist {\n    list-style-type: none;\n}\n.flex-container {\nmax-width:850px;\nmargin-left:auto;\nmargin-right:auto;\n}\n#flag_python {text-align:center;}\n.body {width: 99%;}\n<\/style>\n\n\n<div class=\"flex-container\">\n\n\n\t<div class=\"body\">\n\n\n\n\t\t<div class=\"regex-space-top\"><b>Your regular expression to test<\/b><\/div>\n\t\t<div class=\"editor-container-regexp\">\n\t\t\t<textarea id=\"editor-container-regexp\" class=\"editor\" placeholder=\"Enter your regex\">I (love|hate) Python<\/textarea>\n\t\t<\/div>\n\t\t<div>\n<br\/>\n\t\t\t<div id=\"flag_python\">\n\n\n<input class=\"noflag\" type=\"button\" name=\"flag_i\" id=\"flag_i\" data-value=\"i\" value=\"IGNORECASE\" \/>\n<input class=\"noflag\" type=\"button\" name=\"flag_m\" id=\"flag_m\" data-value=\"m\" value=\"MULTILINE\" \/>\n<input class=\"noflag\" type=\"button\" name=\"flag_l\" id=\"flag_l\" data-value=\"l\" value=\"LOCALE\" \/>\n<input class=\"noflag\" type=\"button\" name=\"flag_u\" id=\"flag_u\" data-value=\"u\" value=\"UNICODE\" \/>\n<input class=\"noflag\" type=\"button\" name=\"flag_s\" id=\"flag_s\" data-value=\"s\" value=\"DOTALL\" \/>\n<input class=\"noflag\" type=\"button\" name=\"flag_x\" id=\"flag_x\" data-value=\"x\" value=\"VERBOSE\" \/>\n\n<!--input class=\"noflag\" type=\"button\" name=\"flag_g\" id=\"flag_g\" data-value=\"g\" value=\"Global search *\" \/-->\n\n\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t<div class=\"regex-space-top\"><b>Your test string<\/b><\/div>\n\n\t\t<div class=\"editor-container\">\n\t\t\t<textarea id=\"editor-container\" class=\"editor\" placeholder=\"Enter your test string\">What else can I say besides I love Python!<\/textarea>\n\t\t<\/div>\n\t\t<div class=\"editor-msg\">\n\t\t\t<div id=\"editor-error\" class=\"editor-error\" style=\"font-family:courier;\"><\/div>\n\t\t\t<div id=\"editor-valid\" class=\"editor-valid\"><\/div>\n\t\t<\/div>\n\n\t\t<br\/>\n\t\t<div id=\"graphCt\" class=\"explanation\"><\/div>\n\t\t<br\/>\n<div class=\"middle\">\n   <input type=\"button\" class=\"action-button-editor span\" value=\"Download JPG\" onClick=\"downloadImage()\" \/>\n\n <input type=\"button\" class=\"action-button-editor span\" value=\"Copy permalink\" onClick=\"copyPermalink()\" \/>\n\n<\/div>\n\n\t\t<!--div class=\"editor-container\">\n\t\t\t<textarea id=\"editor-container-substitution\" class=\"editor\" placeholder=\"<cbo_param_enter_substitution_string>\"><\/textarea>\n\t\t<\/div>\n\t\t<div class=\"editor-container\">\n\t\t\t<textarea id=\"editor-container-substitution-result\" class=\"editor\"><\/textarea>\n\t\t<\/div-->\n\t\t\n\t\t<br\/>\n\t<\/div>\n<\/div>\n<!--\n * The 'global search' functionality is not represented by a flag; instead, it corresponds to the 'finditer' function, which enables the discovery of all matches rather than halting after the initial one.\n-->\n\n\n\n<h2 class=\"wp-block-heading\">Python regex checker<\/h2>\n\n\n\n<p>Welcome to <strong>Pyrexp<\/strong>, our <strong>Python regex testing tool<\/strong>! Pyrexp strive to have a user-friendly interface to make testing your regular expressions more enjoyable. We know that regex can sometimes be hair-pullingly frustrating; we've been there ourselves!<\/p>\n\n\n\n<p>With Pyrexp, you can ensure that your <strong>regex patterns<\/strong> have <strong>valid syntax<\/strong>, eliminating common <strong>syntax errors<\/strong> that often plague regex (Personally, I get lost in those parentheses\u2026 It reminds me of LISP\u2026). Simply input your regular expression, and our tester will validate its syntax.<\/p>\n\n\n\n<p>But that's not all \u2013 it allows you to test your regular expressions against strings, so you can see exactly how they match and identify any potential issues.<\/p>\n\n\n\n<p>Pyrexp is also a <strong>Regex visualizer<\/strong>, it provides <strong>graphical visualization of your regex patterns<\/strong>. This visual representation offers a clear understanding of how your pattern works, making it easier to debug and optimize for your specific needs. Again, those parentheses can be confusing\u2026<\/p>\n\n\n\n<p>And if you need to share your regex pattern or keep it for future reference, our tool allows you to export it as a JPG image. This functionality can be handy for sharing your regex on forums (Yes, ChatGPT hasn't completely replaced them).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Mini tutorial on Regex in Python<\/strong><\/h2>\n\n\n\n<p>Regular expressions (regex) are a powerful way to search, filter, or modify strings. Python provides a built-in module called <code>re<\/code> to work with regex. The <code>re<\/code> library is easy to use, and we will go over its basics together.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Importing the <code>re<\/code> module<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\r<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Searching for a pattern with <code>re.search()<\/code><\/h3>\n\n\n\n<p>The <code>re.search()<\/code> function finds the first match of a pattern in a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\n\ntext = \"Hello, my phone number is 07 45 89 23 67.\"\npattern = r\"\\d{2} \\d{2} \\d{2} \\d{2} \\d{2}\"  # French phone number\n\nresult = re.search(pattern, text)\n\nif result:\n    print(\"Found number:\", result.group())  # Output: \"07 45 89 23 67\"\nelse:\n    print(\"No number found.\")\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using <code>re.findall()<\/code> to find all matches<\/h3>\n\n\n\n<p>The <code>re.findall()<\/code> function finds all matches of a pattern in a string and returns them as a list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\r\n\r\ntext = \"Here are two numbers: 07 45 89 23 67 and 06 12 34 56 78.\"\r\npattern = r\"\\d{2} \\d{2} \\d{2} \\d{2} \\d{2}\"\r\n\r\nnumbers = re.findall(pattern, text)\r\n\r\nprint(\"Found numbers:\", numbers)  # Output: &#91;'07 45 89 23 67', '06 12 34 56 78']\r<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Replacing text with <code>re.sub()<\/code><\/h3>\n\n\n\n<p>The <code>re.sub()<\/code> function replaces occurrences of a pattern with a new string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\r\n\r\ntext = \"Hello, here is my number: 07 45 89 23 67.\"\r\npattern = r\"\\d{2} \\d{2} \\d{2} \\d{2} \\d{2}\"\r\nnew_text = re.sub(pattern, \"&#91;NUMBER MASKED]\", text)\r\n\r\nprint(new_text)  # Output: \"Hello, here is my number: &#91;NUMBER MASKED].\"\r<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Capturing groups with parentheses <code>( )<\/code><\/h3>\n\n\n\n<p>Groups in regular expressions allow you to capture and extract specific parts of a match.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\r\n\r\ntext = \"My email is contact@pythonium.net.\"\r\npattern = r\"(\\w+)@(\\w+\\.\\w+)\"  # Group for the username and domain\r\n\r\nresult = re.search(pattern, text)\r\n\r\nif result:\r\n    print(\"Username:\", result.group(1))  # contact\r\n    print(\"Domain:\", result.group(2))  # pythonium.net\r\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Splitting a string with <code>re.split()<\/code><\/h3>\n\n\n\n<p>The <code>re.split()<\/code> function splits a string into a list at each match of a pattern.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\n\ntext = \"one-two-three-four\"\npattern = r\"-\"\nwords = re.split(pattern, text)\n\nprint(words)  # Output: &#91;'one', 'two', 'three', 'four']<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why test Python regex?<\/h2>\n\n\n\n<p>As a joke, we sometimes say that <strong>testing is doubting<\/strong>\u2026 Unfortunately, with regex, we don't really have a choice; we have to test them to make sure we don't mess up!<\/p>\n\n\n\n<p>You may want to ensure your <strong>regular expressions function correctly in Python<\/strong>, verifying their syntax and confirming their matches against strings. Additionally, testing allows you to visualize and export your regex, aiding in understanding and sharing your patterns effectively.<\/p>\n\n\n\n<p>Pyrexp uses <a href=\"https:\/\/github.com\/CJex\/regulex\" target=\"_blank\" rel=\"noopener\" title=\"\">Regulex<\/a> (Unfortunately, it doesn't seem to be maintained anymore), for the visualization of regular expressions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Your regular expression to test I (love|hate) Python Your test string What else can I say besides I love Python! Python regex checker Welcome to Pyrexp, our Python regex testing tool! Pyrexp strive to have a user-friendly interface to make testing your regular expressions more enjoyable. We know that regex can sometimes be hair-pullingly frustrating; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pythonium.net\/wp-json\/wp\/v2\/pages\/89"}],"collection":[{"href":"https:\/\/pythonium.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pythonium.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pythonium.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonium.net\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":47,"href":"https:\/\/pythonium.net\/wp-json\/wp\/v2\/pages\/89\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/pythonium.net\/wp-json\/wp\/v2\/pages\/89\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/pythonium.net\/wp-json\/wp\/v2\/media?parent=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}