{"id":387,"date":"2024-11-07T08:05:20","date_gmt":"2024-11-07T08:05:20","guid":{"rendered":"https:\/\/techkubo.com\/python\/?p=387"},"modified":"2025-06-20T17:53:58","modified_gmt":"2025-06-20T17:53:58","slug":"python-regular-expressions-regex","status":"publish","type":"post","link":"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/","title":{"rendered":"Python Regular Expressions (RegEx)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is Regular Expression?<\/h2>\n\n\n\n<p><strong>Regular Expressions<\/strong> or <strong>RegEx<\/strong> are special sequences of characters used to search for patterns within text. It works like a &#8220;search tool&#8221; that allows you to find specific strings or patterns thus making working with text data easier.<br><br>For example, if you want to search the word &#8220;tagumpay&#8221; (success in Filipino) in a text file you can easily look for it with <strong>RegEx<\/strong> by creating a pattern that will look for the words that matches &#8220;tagumpay&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use RegEx?<\/h2>\n\n\n\n<p>RegEx can be really helpful in many use-cases: <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Text Searching<\/strong>: Find specific words or patterns within a string.<\/li>\n\n\n\n<li><strong>Data Validation<\/strong>: Validate specific formats like email addresses, phone numbers, postal codes, and etc.<\/li>\n\n\n\n<li><strong>Data Cleaning<\/strong>: Remove unwanted characters or whitespace from text.<\/li>\n\n\n\n<li><strong>Data Extraction<\/strong>: Extract specific information from large datasets such as dates, prices, or keywords.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Start Guide to Regular Expressions Metacharacters, Special Sequences, and Sets<\/h2>\n\n\n\n<p><strong>Metacharacters: Your &#8220;Pattern-Making Tools&#8221;<\/strong><br><br>In RegEx, <strong>metacharacters <\/strong>are special symbols that help define the pattern we are looking for. They work as shortcuts for specifying the &#8220;rules&#8221; of your pattern<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Character<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Example<\/strong><\/td><td><strong>Explanation<\/strong><\/td><\/tr><tr><td>[ ]<\/td><td>A set of specific characters<\/td><td>[aeiou]<\/td><td>Matches any vowel (a, e, i, o, or u).<\/td><\/tr><tr><td>\\<\/td><td>Signals a special sequence (or &#8220;escapes&#8221; special characters)<\/td><td>\\d<\/td><td>Matches any digit (0-9), or lets you use symbols like <code>\\.<\/code> to match a literal period.<\/td><\/tr><tr><td>.<\/td><td>Any character except a newline<\/td><td>a.b<\/td><td>Matches &#8220;a&#8221; followed by any character and then &#8220;b&#8221; (e.g., &#8220;acb,&#8221; &#8220;a1b,&#8221; &#8220;a-b&#8221;).<\/td><\/tr><tr><td>^<\/td><td>Matches if the text starts with this pattern<\/td><td>^Hello<\/td><td>Matches any string that starts with &#8220;Hello&#8221;.<\/td><\/tr><tr><td>$<\/td><td>Matches if the text ends with this pattern<\/td><td>world$<\/td><td>Matches any string that ends with &#8220;world&#8221;.<\/td><\/tr><tr><td>*<\/td><td>Zero or more occurrences of the previous character<\/td><td>ba*<\/td><td>Matches &#8220;b,&#8221; &#8220;ba,&#8221; &#8220;baa,&#8221; and so on.<\/td><\/tr><tr><td>+<\/td><td>One or more occurrences of the previous character<\/td><td>ba+<\/td><td>Matches &#8220;ba,&#8221; &#8220;baa,&#8221; &#8220;baaa,&#8221; etc., but not just &#8220;b&#8221;.<\/td><\/tr><tr><td>?<\/td><td>Zero or one occurrence of the previous character<\/td><td>colou?r<\/td><td>Matches &#8220;color&#8221; or &#8220;colour&#8221;.<\/td><\/tr><tr><td>{ }<\/td><td>Matches an exact number of occurrences<\/td><td>\\d{3}<\/td><td>Matches exactly 3 digits (e.g., &#8220;123&#8221; but not &#8220;12&#8221;).<\/td><\/tr><tr><td>`<\/td><td>`<\/td><td>Acts as an &#8220;or&#8221; between patterns<\/td><td>`cat<\/td><\/tr><tr><td>( )<\/td><td>Groups patterns together<\/td><td>(abc)+<\/td><td>Matches &#8220;abc,&#8221; &#8220;abcabc,&#8221; etc., treating &#8220;abc&#8221; as a unit.<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Quick guide to some of the most useful metacharacters:<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Special Sequences: Quick Pattern Shortcuts<\/strong><br><br><strong>Special sequences <\/strong>start with <strong>\\<\/strong> and let you search for particular types of characters.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Character<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Example<\/strong><\/td><td><strong>Explanation<\/strong><\/td><\/tr><tr><td>\\A<\/td><td>Matches if the text starts with this pattern<\/td><td>\\AOnce<\/td><td>Matches any string that starts with &#8220;Once&#8221;.<\/td><\/tr><tr><td>\\b<\/td><td>Matches at the beginning or end of a word<\/td><td>r&#8221;\\bhero\\b&#8221;<\/td><td>Matches &#8220;hero&#8221; as a whole word, not inside words like &#8220;superhero&#8221;.<\/td><\/tr><tr><td>\\B<\/td><td>Matches inside a word (not at boundaries)<\/td><td>r&#8221;\\Bhero&#8221;<\/td><td>Matches &#8220;hero&#8221; inside other words, like &#8220;superhero&#8221;.<\/td><\/tr><tr><td>\\d<\/td><td>Matches any digit (0\u20139)<\/td><td>&#8220;\\d+&#8221;<\/td><td>Matches any sequence of digits (e.g., &#8220;123&#8221;).<\/td><\/tr><tr><td>\\D<\/td><td>Matches any non-digit<\/td><td>&#8220;\\D+&#8221;<\/td><td>Matches any sequence of non-digits (e.g., &#8220;abc&#8221;).<\/td><\/tr><tr><td>\\s<\/td><td>Matches any whitespace (space, tab, newline)<\/td><td>&#8220;\\s&#8221;<\/td><td>Useful for finding spaces or line breaks.<\/td><\/tr><tr><td>\\S<\/td><td>Matches any non-whitespace<\/td><td><code>\"\\S+\"<\/code><\/td><td>Useful for matching words or characters with no spaces.<\/td><\/tr><tr><td>\\w<\/td><td>Matches any &#8220;word&#8221; character (letters, digits, underscore)<\/td><td>&#8220;\\w+&#8221;<\/td><td>Useful for matching words or variable names.<\/td><\/tr><tr><td>\\W<\/td><td>Matches any non-word character<\/td><td>&#8220;\\W+&#8221;<\/td><td>Matches spaces, punctuation, etc.<\/td><\/tr><tr><td>\\Z<\/td><td>Matches if the text ends with this pattern<\/td><td>r&#8221;end\\Z&#8221;<\/td><td>Matches any string ending with &#8220;end&#8221;.<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Quick guide to some of the most useful special sequences<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Sets: Picking Specific Characters<\/strong><br><br><strong>Sets<\/strong> help you specify a group of characters that you want to match. They&#8217;re wrapped in square brackets <strong>[ ]<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Character<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Example<\/strong><\/td><td><strong>Explanation<\/strong><\/td><\/tr><tr><td>[aeiou]<\/td><td>Matches any of the specified characters<\/td><td>[aeiou]<\/td><td>Matches any lowercase vowel.<\/td><\/tr><tr><td>[a-z]<\/td><td>Matches any character in this range<\/td><td>[a-z]<\/td><td>Matches any lowercase letter.<\/td><\/tr><tr><td>[^aeiou]<\/td><td>Matches any character <em>except<\/em> those specified<\/td><td>[^aeiou]<\/td><td>Matches any character that is not a lowercase vowel.<\/td><\/tr><tr><td>[0-9]<\/td><td>Matches any digit from 0 to 9<\/td><td>[0-9]<\/td><td>Matches any single digit.<\/td><\/tr><tr><td>[a-Za-z]<\/td><td>Matches any uppercase or lowercase letter<\/td><td>[A-Za-z]<\/td><td>Matches any letter, regardless of case.<\/td><\/tr><tr><td>[+]<\/td><td>In sets, special characters lose their meaning<\/td><td>[+]<\/td><td>Matches the plus sign literally.<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Quick guide to some of the most useful special sequences<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to use RegEx in Python?<\/h2>\n\n\n\n<p>Python has a built-in module called <code>re<\/code> that includes many functions to work with RegEx patterns.<\/p>\n\n\n\n<p><strong>Import the re module<\/strong>: Before using RegEx, import the <strong>re <\/strong>module.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import re<\/pre><\/div>\n\n\n\n<p><strong>Defining Patterns<\/strong>: Define a pattern then use the functions provided by the <strong>re <\/strong>module match, search, or replace text.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<p><strong>Function 1: re.match()<\/strong><br><br>The <strong>re.match()<\/strong> function checks if the pattern matches only at the start of the string.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;seti&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import re\n\n# Example text mentioning tinola\ntext = &quot;Tinola ang paboritong ulam ng ating pambansang bayani na si Dr. Jose Riza&quot;\n\n# Pattern to check if &quot;tinola&quot; appears at the start of the text\npattern = r&quot;^tinola&quot;\n\nresult = re.match(pattern, text, re.IGNORECASE) # Using re.IGNORECASE to make it case-insensitive\nif result: \n  print(&quot;Match found: &quot;, result.group())  # Output: &quot;Tinola&quot;\nelse: \n  print(&quot;No match at the start.&quot;)<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Function 2: re.search() <\/strong><br><br>The <strong>re.search() <\/strong>functions scans through the entire text to find the first occurrence of a pattern.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;seti&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import re\n\n# Text with multiple mentions of Katipunan\ntext = &quot;&quot;&quot;Ang Katipunan ay isang lihim na samahan noong panahon ng rebolusyon. \nIsa sa mga layunin ng Katipunan ay ang kasarinlan ng Pilipinas.&quot;&quot;&quot;\n\n# Pattern to find the word &quot;Katipunan&quot; anywhere in the text\npattern = r&quot;Katipunan&quot;\n\nresult = re.search(pattern, text)\n\nif result: \n  print(&quot;Found: &quot;, result.group()) # Output: &quot;Katipunan&quot;\nelse:\n  print(&quot;No match found.&quot;)<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Function 3: re.findall() <\/strong><br><br>The <strong>re.findall() <\/strong>function is used to find all occurrences of a pattern in the text.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;seti&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import re\n\n# Text with honorifics\ntext = &quot;Si Ginoo Juan at Aling Maria ay nagtutulungan sa bayan.&quot;\n\n# Pattern to find Filipino honorifics &quot;Ginoo&quot; and &quot;Aling&quot;\npattern = r&quot;\\b(Ginoo|Aling) \\w+\\b&quot;\n\nresult = re.findall(pattern, text)\nprint(&quot;Honorifics found:&quot;, result)  # Output: [&quot;Ginoo Juan&quot;, &quot;Aling Maria&quot;]<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Function 4: re.sub()<\/strong><br><br>The <strong>re.sub()<\/strong> function replaces all matches of a pattern with a specified replacement string.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;seti&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import re\n\n# Sample text with polite terms\ntext = &quot;Maraming salamat po! Ako po si Ginoo Rizal. Pumunta po ako para magbayad ng sedula.&quot;\n\n# Pattern to replace &quot;po&quot; and &quot;opo&quot; with &quot;Please&quot;\npattern = r&quot;\\b(po|opo)\\b&quot;\nnew_text = re.sub(pattern, &quot;Please&quot;, text)\n\nprint(&quot;Adjusted text:\\n&quot;, new_text)\n# Output: &quot;Maraming salamat Please! Ako Please si Ginoo Rizal. Pumunta Please ako para magbayad ng sedula.&quot;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Try this out!<\/strong><\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;seti&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import re\n\ntext = &quot;&quot;&quot;Ang galing ng mga Pilipino! \nIsang malaking tagumpay ang parangal na natanggap nila. \nAng parangal na ito ay tunay na karapat-dapat.&quot;&quot;&quot;\n\n# Step 1: Define a pattern to match success-related words\nsuccess_pattern = r&quot;\\b(galing|tagumpay|parangal)\\b&quot;\n\n# Step 2: Find all success-related words\nmatches = re.findall(success_pattern, text)\nprint(&quot;Filipino excellence words found:&quot;, matches)\n# Output: [&quot;galing&quot;, &quot;tagumpay&quot;, &quot;parangal&quot;, &quot;parangal&quot;]\n\n# Step 3: Replace &quot;parangal&quot; with &quot;prestihiyosong parangal&quot; for emphasis\ntext_with_emphasis = re.sub(r&quot;\\bparangal\\b&quot;, &quot;prestihiyosong parangal&quot;, text)\nprint(&quot;Text with emphasized achievement:\\n&quot;, text_with_emphasis)\n# Output: &quot;Ang galing ng mga Pilipino! Isang malaking tagumpay ang prestihiyosong parangal na natanggap nila. Ang prestihiyosong parangal na ito ay tunay na karapat-dapat.&quot;\n\n# Step 4: Check if the text starts with &quot;Ang galing&quot;\nif re.match(r&quot;^Ang galing&quot;, text):\n    print(&quot;The text begins with 'Ang galing' - a phrase highlighting excellence.&quot;)\nelse:\n    print(&quot;The text does not start with 'Ang galing'.&quot;)\n\n# Step 5: Search for the first occurrence of any success-related word\nfirst_success_word = re.search(success_pattern, text)\nif first_success_word:\n    print(&quot;First success-related word found:&quot;, first_success_word.group())\n# Output: &quot;galing&quot;\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><iframe title=\"Try Python on TechKubo\" src=\"https:\/\/techkubo.com\/python-sim.html\" width=\"100%\" height=\"500\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Regular Expression? Regular Expressions or RegEx are special sequences of characters used to search for patterns within text. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-387","post","type-post","status-publish","format-standard","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Regular Expressions (RegEx) - Python Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Regular Expressions (RegEx) - Python Tutorial\" \/>\n<meta property=\"og:description\" content=\"What is Regular Expression? Regular Expressions or RegEx are special sequences of characters used to search for patterns within text. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-07T08:05:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-20T17:53:58+00:00\" \/>\n<meta name=\"author\" content=\"Manong\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Manong\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/\"},\"author\":{\"name\":\"Manong\",\"@id\":\"https:\/\/techkubo.com\/python\/#\/schema\/person\/b4fa2f01fa4ff2a4e98276ce47115965\"},\"headline\":\"Python Regular Expressions (RegEx)\",\"datePublished\":\"2024-11-07T08:05:20+00:00\",\"dateModified\":\"2025-06-20T17:53:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/\"},\"wordCount\":803,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/techkubo.com\/python\/#organization\"},\"articleSection\":[\"python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/\",\"url\":\"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/\",\"name\":\"Python Regular Expressions (RegEx) - Python Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/techkubo.com\/python\/#website\"},\"datePublished\":\"2024-11-07T08:05:20+00:00\",\"dateModified\":\"2025-06-20T17:53:58+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/techkubo.com\/python\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Regular Expressions (RegEx)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/techkubo.com\/python\/#website\",\"url\":\"https:\/\/techkubo.com\/python\/\",\"name\":\"Python Tutorial\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/techkubo.com\/python\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/techkubo.com\/python\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/techkubo.com\/python\/#organization\",\"name\":\"Python Tutorial\",\"url\":\"https:\/\/techkubo.com\/python\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/techkubo.com\/python\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/techkubo.com\/python\/wp-content\/uploads\/sites\/2\/2025\/01\/cropped-Techkubo-logo-1.png\",\"contentUrl\":\"https:\/\/techkubo.com\/python\/wp-content\/uploads\/sites\/2\/2025\/01\/cropped-Techkubo-logo-1.png\",\"width\":1620,\"height\":1156,\"caption\":\"Python Tutorial\"},\"image\":{\"@id\":\"https:\/\/techkubo.com\/python\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/techkubo.com\/python\/#\/schema\/person\/b4fa2f01fa4ff2a4e98276ce47115965\",\"name\":\"Manong\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/techkubo.com\/python\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/21a7455736c21887b8fefe0935012d65?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/21a7455736c21887b8fefe0935012d65?s=96&d=mm&r=g\",\"caption\":\"Manong\"},\"sameAs\":[\"https:\/\/techkubo.com\"],\"url\":\"https:\/\/techkubo.com\/python\/author\/manong\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Regular Expressions (RegEx) - Python Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/","og_locale":"en_US","og_type":"article","og_title":"Python Regular Expressions (RegEx) - Python Tutorial","og_description":"What is Regular Expression? Regular Expressions or RegEx are special sequences of characters used to search for patterns within text. [&hellip;]","og_url":"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/","og_site_name":"Python Tutorial","article_published_time":"2024-11-07T08:05:20+00:00","article_modified_time":"2025-06-20T17:53:58+00:00","author":"Manong","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Manong","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/#article","isPartOf":{"@id":"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/"},"author":{"name":"Manong","@id":"https:\/\/techkubo.com\/python\/#\/schema\/person\/b4fa2f01fa4ff2a4e98276ce47115965"},"headline":"Python Regular Expressions (RegEx)","datePublished":"2024-11-07T08:05:20+00:00","dateModified":"2025-06-20T17:53:58+00:00","mainEntityOfPage":{"@id":"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/"},"wordCount":803,"commentCount":0,"publisher":{"@id":"https:\/\/techkubo.com\/python\/#organization"},"articleSection":["python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/","url":"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/","name":"Python Regular Expressions (RegEx) - Python Tutorial","isPartOf":{"@id":"https:\/\/techkubo.com\/python\/#website"},"datePublished":"2024-11-07T08:05:20+00:00","dateModified":"2025-06-20T17:53:58+00:00","breadcrumb":{"@id":"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/techkubo.com\/python\/python-regular-expressions-regex\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techkubo.com\/python\/"},{"@type":"ListItem","position":2,"name":"Python Regular Expressions (RegEx)"}]},{"@type":"WebSite","@id":"https:\/\/techkubo.com\/python\/#website","url":"https:\/\/techkubo.com\/python\/","name":"Python Tutorial","description":"","publisher":{"@id":"https:\/\/techkubo.com\/python\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techkubo.com\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techkubo.com\/python\/#organization","name":"Python Tutorial","url":"https:\/\/techkubo.com\/python\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techkubo.com\/python\/#\/schema\/logo\/image\/","url":"https:\/\/techkubo.com\/python\/wp-content\/uploads\/sites\/2\/2025\/01\/cropped-Techkubo-logo-1.png","contentUrl":"https:\/\/techkubo.com\/python\/wp-content\/uploads\/sites\/2\/2025\/01\/cropped-Techkubo-logo-1.png","width":1620,"height":1156,"caption":"Python Tutorial"},"image":{"@id":"https:\/\/techkubo.com\/python\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/techkubo.com\/python\/#\/schema\/person\/b4fa2f01fa4ff2a4e98276ce47115965","name":"Manong","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techkubo.com\/python\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/21a7455736c21887b8fefe0935012d65?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/21a7455736c21887b8fefe0935012d65?s=96&d=mm&r=g","caption":"Manong"},"sameAs":["https:\/\/techkubo.com"],"url":"https:\/\/techkubo.com\/python\/author\/manong\/"}]}},"_links":{"self":[{"href":"https:\/\/techkubo.com\/python\/wp-json\/wp\/v2\/posts\/387"}],"collection":[{"href":"https:\/\/techkubo.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techkubo.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techkubo.com\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techkubo.com\/python\/wp-json\/wp\/v2\/comments?post=387"}],"version-history":[{"count":4,"href":"https:\/\/techkubo.com\/python\/wp-json\/wp\/v2\/posts\/387\/revisions"}],"predecessor-version":[{"id":701,"href":"https:\/\/techkubo.com\/python\/wp-json\/wp\/v2\/posts\/387\/revisions\/701"}],"wp:attachment":[{"href":"https:\/\/techkubo.com\/python\/wp-json\/wp\/v2\/media?parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techkubo.com\/python\/wp-json\/wp\/v2\/categories?post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techkubo.com\/python\/wp-json\/wp\/v2\/tags?post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}