{"id":86,"date":"2025-10-25T06:47:09","date_gmt":"2025-10-25T06:47:09","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=86"},"modified":"2025-12-17T07:49:06","modified_gmt":"2025-12-17T07:49:06","slug":"regular-expressions","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=86","title":{"rendered":"Lesson 21: Regular Expressions in Python"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p>To understand and apply <strong>Regular Expressions (RegEx)<\/strong> in Python for searching, validating, and manipulating text patterns efficiently.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. What Are Regular Expressions?<\/strong><\/h2>\n\n\n\n<p><strong>Regular Expressions (RegEx)<\/strong> are special patterns used to match strings or parts of strings.<br>Python provides the <strong><code>re<\/code><\/strong> module to work with these expressions.<\/p>\n\n\n\n<p>\u2705 <strong>Importing RegEx Module:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Common RegEx Functions<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Function<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>re.search()<\/code><\/td><td>Finds the <strong>first match<\/strong> in a string<\/td><\/tr><tr><td><code>re.findall()<\/code><\/td><td>Returns <strong>all matches<\/strong> as a list<\/td><\/tr><tr><td><code>re.match()<\/code><\/td><td>Checks <strong>if string starts<\/strong> with the pattern<\/td><\/tr><tr><td><code>re.split()<\/code><\/td><td>Splits string by the matched pattern<\/td><\/tr><tr><td><code>re.sub()<\/code><\/td><td>Replaces pattern with a new string<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd0d <strong>3. Basic Examples<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1 \u2013 <code>search()<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\ntext = \"My phone number is 9876543210\"\npattern = r\"\\d{10}\"\nmatch = re.search(pattern, text)\nprint(match.group())\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>9876543210\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2 \u2013 <code>findall()<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Emails: user1@gmail.com, user2@yahoo.com\"\nemails = re.findall(r'\\S+@\\S+', text)\nprint(emails)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'user1@gmail.com', 'user2@yahoo.com']\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3 \u2013 <code>match()<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Python is powerful\"\nresult = re.match(r\"Python\", text)\nprint(result)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;re.Match object; span=(0, 6), match='Python'&gt;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4 \u2013 <code>split()<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"apple,banana;orange grape\"\nwords = re.split(r'&#91;;,\\s]+', text)\nprint(words)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'apple', 'banana', 'orange', 'grape']\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example 5 \u2013 <code>sub()<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"My number is 9876543210\"\nnew_text = re.sub(r'\\d+', '**********', text)\nprint(new_text)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>My number is **********\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 <strong>4. Common RegEx Patterns<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Pattern<\/th><th>Meaning<\/th><th>Example Match<\/th><\/tr><\/thead><tbody><tr><td><code>\\d<\/code><\/td><td>Digit (0\u20139)<\/td><td>3<\/td><\/tr><tr><td><code>\\D<\/code><\/td><td>Non-digit<\/td><td>A<\/td><\/tr><tr><td><code>\\w<\/code><\/td><td>Word character (a\u2013z, 0\u20139, _)<\/td><td>a<\/td><\/tr><tr><td><code>\\W<\/code><\/td><td>Non-word character<\/td><td>@<\/td><\/tr><tr><td><code>\\s<\/code><\/td><td>Whitespace<\/td><td>(space, tab)<\/td><\/tr><tr><td><code>^<\/code><\/td><td>Start of string<\/td><td><code>^Python<\/code><\/td><\/tr><tr><td><code>$<\/code><\/td><td>End of string<\/td><td><code>end$<\/code><\/td><\/tr><tr><td><code>.<\/code><\/td><td>Any character (except newline)<\/td><td><code>c.t<\/code> \u2192 cat, cot<\/td><\/tr><tr><td><code>*<\/code><\/td><td>0 or more repetitions<\/td><td><code>lo*l<\/code> \u2192 ll, lol, loool<\/td><\/tr><tr><td><code>+<\/code><\/td><td>1 or more repetitions<\/td><td><code>go+<\/code> \u2192 go, goo, gooo<\/td><\/tr><tr><td><code>{n}<\/code><\/td><td>Exactly n times<\/td><td><code>\\d{4}<\/code> \u2192 2025<\/td><\/tr><tr><td>`<\/td><td>`<\/td><td>OR condition<\/td><\/tr><tr><td><code>[]<\/code><\/td><td>Character set<\/td><td><code>[A-Z]<\/code> matches A\u2013Z<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 <strong>5. Real-Life Use Cases<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Validate <strong>email<\/strong>, <strong>phone number<\/strong>, or <strong>passwords<\/strong><\/li>\n\n\n\n<li>Extract <strong>data<\/strong> from text (e.g., IDs, numbers, dates)<\/li>\n\n\n\n<li>Find or replace text in logs or documents<\/li>\n\n\n\n<li>Split structured text (CSV, JSON strings)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf0d <strong>6. Real-Life Example \u2013 Email Validation<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\n\nemail = \"sameer123@gmail.com\"\npattern = r'^&#91;a-zA-Z0-9._]+@&#91;a-z]+\\.&#91;a-z]+$'\n\nif re.match(pattern, email):\n    print(\"Valid email address!\")\nelse:\n    print(\"Invalid email address!\")\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Valid email address!\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcac <strong>7. Password Strength Validation<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>password = \"Pa$$word123\"\npattern = r'^(?=.*&#91;A-Z])(?=.*&#91;a-z])(?=.*\\d)(?=.*&#91;@$!%*?&amp;])&#91;A-Za-z\\d@$!%*?&amp;]{8,}$'\n\nif re.match(pattern, password):\n    print(\"Strong password!\")\nelse:\n    print(\"Weak password!\")\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Strong password!\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand and apply Regular Expressions (RegEx) in Python for searching, validating, and manipulating text patterns efficiently. \ud83e\udde9 1. What Are Regular Expressions? Regular Expressions (RegEx) are special patterns used to match strings or parts of strings.Python provides the re module to work with these expressions. \u2705 Importing RegEx Module: \u2699\ufe0f 2. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=86"}],"version-history":[{"count":2,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":93,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/86\/revisions\/93"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}