{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h2 align=\"center\"><font color='black'>Introduction to Regular Expressions</font></h2>\n\nRegular expressions (regex or regexp) are powerful tools used for pattern matching and searching within text. They are widely used in various programming languages and tools to perform tasks like validation, data extraction, and text manipulation. Regex patterns are composed of a combination of characters and symbols that define a specific pattern to search for within a given text.\n\nLearning regular expressions *(regex)* is a valuable skill that can greatly enhance your ability to work with text data efficiently and effectively. \n\n<center>\n<img src=\"https://www.oreilly.com/content/wp-content/uploads/sites/2/2019/06/email-regex_crop-ae942dc427c8cebd3a83c52d17389123.jpg\" width=300>\n</center>","metadata":{}},{"cell_type":"markdown","source":"### <font color='black'>Table of contents<font><a class='anchor' id='top'></a>\n1. [Importance of Regular Expressions](#importance)\n2. [`re` libray](#re)\n3. [Pattern 1: Matching Phone Numbers](#pattern1)\n4. [Pattern 2: Matching Names with Phone Numbers](#pattern2)\n5. [Pattern 3: Matching Email Addresses](#pattern3)\n6. [But why is there an `r` before the pattern?](#whyr)\n7. [Pattern 4: Matching URLs](#pattern4)\n8. [Pattern 5: Matching Dates](#pattern5)\n9. [Pattern 6: Matching Any Character](#pattern6)\n10. [Where to practice?](#practice)\n11. [Conclusion](#conclusion)","metadata":{}},{"cell_type":"markdown","source":"## Importance of Regular Expressions <a class='anchor' id='importance'></a> [↑](#top)\n\nRegular expressions are a versatile and powerful tool used for text processing, pattern matching, and data manipulation. Here are some key reasons why learning regular expressions is important:\n\n1. **Efficient Text Processing:** Regular expressions provide a concise and flexible way to search, extract, and manipulate text data. They allow you to perform complex operations with just a few lines of code.\n\n2. **Pattern Matching:** One of the primary uses of regex is to find patterns within text. This ability is crucial for tasks like data validation, searching for keywords, and extracting specific information from large datasets.\n\n3. **Data Validation:** Regular expressions are commonly used to validate user input in applications such as form validation, password validation, and email validation. This ensures that the data adheres to a certain format or structure.\n\n4. **Text Extraction and Manipulation:** With regex, you can easily extract specific pieces of information from unstructured text, such as phone numbers, email addresses, URLs, and more. You can also replace or transform text based on patterns, saving time and effort.\n\n5. **Automating Tedious Tasks:** Regular expressions can help automate tasks that involve text manipulation. For example, you can use regex to clean up messy data, reformat text, or extract information from logs.\n\n6. **Text Parsing:** Many programming languages and tools support regex for parsing structured text formats like JSON, XML, HTML, and more. This enables you to extract relevant data from these formats efficiently.\n\n7. **Programming and Scripting Languages:** Regular expressions are supported in a wide range of programming languages, including Python, Java, JavaScript, Perl, and more. Learning regex enhances your programming skills and makes you more versatile as a developer.\n\n8. **Data Analysis:** In data analysis and data science, regex can help preprocess and clean text data before further analysis. This is particularly useful for sentiment analysis, text categorization, and natural language processing.\n\n9. **Search and Replace in Editors:** Many text editors and IDEs support regex-based search and replace. This can be incredibly helpful when you're working with large codebases or documents.\n\n10. **Problem-Solving:** Regular expressions challenge your problem-solving skills and logical thinking. They encourage you to think critically about patterns and come up with efficient solutions.\n\n## `re` library <a class='anchor' id='re'></a> [↑](#top)\n\nWe will using the `re` library in Python which is the only import that we need for this notebook. The `re` library in Python is a powerful tool that provides support for working with regular expressions. \"re\" stands for \"regular expression,\" and this library empowers you to search, manipulate, and process text data based on specific patterns. Whether you're validating user input, extracting information from text, or performing complex text transformations, the `re` library is your go-to tool.\n\nKey features and aspects of the `re` library include:\n\n1. **Pattern Matching:** The library enables you to define regular expressions that describe specific patterns you're looking for in text data.\n\n2. **Searching and Matching:** You can use the `re.search()` function to find the first occurrence of a pattern within a string.\n\n3. **Global Search:** The `re.findall()` function helps you find all occurrences of a pattern within a string, returning a list of matches.\n\n4. **Pattern Compilation:** The `re.compile()` function allows you to compile a regular expression pattern into a regular expression object. This can improve efficiency when you need to use the same pattern repeatedly.\n\n5. **Replacing Text:** The `re.sub()` function is used to replace occurrences of a pattern in a string with specified text.\n\n6. **Grouping and Capturing:** Parentheses `( )` in your regular expression patterns allow you to capture specific parts of a match. This is useful for extracting specific information.\n\n7. **Modifiers:** The `re.IGNORECASE` flag can be passed to functions to make pattern matching case-insensitive.\n\n8. **Metacharacters:** The library supports a range of metacharacters, such as `.` (any character except newline), `*` (zero or more occurrences), `+` (one or more occurrences), and more.\n\n9. **Anchors:** Anchors like `^` (start of string) and `$` (end of string) help you match patterns at specific positions within the text.\n\n10. **Escape Sequences:** Special characters, like `\\`, can be escaped using the backslash to match them literally.\n\n11. **Greedy and Non-Greedy Matching:** Quantifiers like `*` and `+` are greedy by default, but you can use `*?` and `+?` for non-greedy matches.\n\nThe `re` library is an essential tool for working with text data, enabling you to manipulate and process text effectively using the power of regular expressions. It's widely used across various domains, including web development, data processing, text analysis, and more. Mastering the `re` library can greatly enhance your ability to work with textual information and solve a wide range of text-related challenges.\n\nLet's go over some regex patterns and see what they mean and how they can be used in our workflows to get the results we are after. Let's begin.","metadata":{}},{"cell_type":"markdown","source":"## Pattern 1: Matching Phone Numbers<a class='anchor' id='pattern1'></a> [↑](#top)\n\nPattern: \n\n```python\n'\\d{3}-\\d{3}-\\d{4}'\n```\n\nExplanation:\n- `\\d`: Matches any digit (equivalent to [0-9]).\n- `{3}`: Specifies that the preceding element (digit) should occur exactly 3 times.\n- `-`: Matches the hyphen character literally.\n- Combining these, `\\d{3}-\\d{3}-\\d{4}` matches phone numbers in the format XXX-XXX-XXXX.","metadata":{}},{"cell_type":"code","source":"import re\n\ntext = \"\"\"\nAlice: 555-123-4567\nBob: 333-987-6543\nCharlie: 777-555-88\n\"\"\"\npattern = r'\\d{3}-\\d{3}-\\d{4}'\nmatches = re.findall(pattern, text)\nprint(\"Pattern 1 Matches:\", matches)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.443003Z","iopub.execute_input":"2023-11-03T12:04:34.444525Z","iopub.status.idle":"2023-11-03T12:04:34.454280Z","shell.execute_reply.started":"2023-11-03T12:04:34.444448Z","shell.execute_reply":"2023-11-03T12:04:34.452443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\">\n<b></b> Notice Charlie's number wasn't matched because the phone number ended with only two digits. While this situation is less likely with actual phone numbers, it demonstrates a limitation of the pattern when dealing with cases where the length of the numbers is uncertain. In scenarios where you're unsure about the exact length of the numbers, this pattern might not provide the desired results, prompting the need to explore alternative regex patterns to address varying number lengths.\n</div>","metadata":{}},{"cell_type":"markdown","source":"## Pattern 2: Matching Names with Phone Numbers\nPattern: \n\n```python \n\"([A-Za-z]+):\\s(\\d{3}-\\d{3}-\\d{4})\"\n```\n\n### Explanation:\n\n- `([A-Za-z]+)`: Capturing group for matching names composed of alphabetic characters.\n- `:\\s:` Matches a colon followed by a space character.\n- `(\\d{3}-\\d{3}-\\d{4})`: Capturing group for matching phone numbers.","metadata":{}},{"cell_type":"code","source":"text = \"\"\"\nAlice: 555-123-4567\nBob: 333-987-6543\nCharlie: 777-555-88\n\"\"\"\npattern = r'([A-Za-z]+):\\s(\\d{3}-\\d{3}-\\d{4})'\nmatches = re.findall(pattern, text)\nprint('Pattern 2 Matches: \\n')\nfor match in matches:\n    print(\"Name:\", match[0], \"\\tPhone:\", match[1])","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.456971Z","iopub.execute_input":"2023-11-03T12:04:34.457615Z","iopub.status.idle":"2023-11-03T12:04:34.470504Z","shell.execute_reply.started":"2023-11-03T12:04:34.457366Z","shell.execute_reply":"2023-11-03T12:04:34.469122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\">\n<b></b> Notice Charlie's number wasn't matched because the phone number ended with only two digits. While this situation is less likely with actual phone numbers, it demonstrates a limitation of the pattern when dealing with cases where the length of the numbers is uncertain. In scenarios where you're unsure about the exact length of the numbers, this pattern might not provide the desired results, prompting the need to explore alternative regex patterns to address varying number lengths.\n</div>","metadata":{}},{"cell_type":"markdown","source":"## Pattern 3: Matching Email Addresses<a class='anchor' id='pattern3'></a> [↑](#top)\nPattern: \n\n```python \n\"\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,7}\\b\"\n```\n\n### Explanation:\n\n- `\\b`: Matches a word boundary to ensure the entire email address is matched.\n- `[A-Za-z0-9._%+-]+`: Matches one or more characters from the allowed set in an email username.\n- `@`: Matches the \"@\" symbol.\n- `[A-Za-z0-9.-]+`: Matches one or more characters in the domain name.\n- `\\.:` Matches a literal period (dot).\n- `[A-Z|a-z]{2,7}`: Matches the top-level domain (TLD) with 2 to 7 alphabetic characters.","metadata":{}},{"cell_type":"code","source":"pattern = r'\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,7}\\b'\nemails = \"\"\"\nEmails: john.doe@example.com, jane_smith@gmail.com, info@company.net\n\"\"\"\nmatches = re.findall(pattern, emails, re.IGNORECASE)\nprint(\"Pattern 3 Matches:\", matches)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.473129Z","iopub.execute_input":"2023-11-03T12:04:34.474197Z","iopub.status.idle":"2023-11-03T12:04:34.486566Z","shell.execute_reply.started":"2023-11-03T12:04:34.474147Z","shell.execute_reply":"2023-11-03T12:04:34.484948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If you just want the user name that is the text before the `@` symbol then we can use the pattern below.","metadata":{}},{"cell_type":"code","source":"pattern = r'\\b([A-Za-z0-9._]+)@\\b'\nemails = \"\"\"\njohn.doe@example.com, jane_smith@gmail.com, info@company.net\n\"\"\"\nmatches = re.findall(pattern, emails, re.IGNORECASE)\nprint(\"Pattern 3 Matches:\", matches)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.490602Z","iopub.execute_input":"2023-11-03T12:04:34.491737Z","iopub.status.idle":"2023-11-03T12:04:34.501781Z","shell.execute_reply.started":"2023-11-03T12:04:34.491680Z","shell.execute_reply":"2023-11-03T12:04:34.499974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's break down the regular expression pattern `r'\\b@([A-Za-z0-9._]+)\\b'` step by step:\n\n1. `\\b`: Word Boundary - `\\b` is a word boundary anchor. It matches the position between a word character (such as letters or digits) and a non-word character (such as spaces, punctuation, or the start/end of a line). It's used to ensure that the pattern matches whole words.\n\n2. `@`: Literal '@' Symbol - The `@` character matches the \"@\" symbol exactly.\n\n3. `([A-Za-z0-9._]+)`: Capturing Group for Email Username - `(` and `)` define a capturing group. This capturing group is used to capture the email username part and `[A-Za-z0-9._]+` matches one or more occurrences of characters that are valid in an email username. This includes alphabetic characters (both uppercase and lowercase), digits, dots, and underscores.\n\n4. `\\b`: Word Boundary - Another word boundary anchor `\\b` is used to ensure the pattern matches whole words.\n\nIf you want to fetch the domain names from an email you can use the below pattern that will escape the `@` symbol and include anything after that.","metadata":{}},{"cell_type":"code","source":"pattern = r'\\b@([A-Za-z0-9._]+)\\b'\nemails = \"\"\"\njohn.doe@example.com, jane_smith@gmail.com, info@company.net\n\"\"\"\nmatches = re.findall(pattern, emails, re.IGNORECASE)\nprint(\"Pattern 3 Matches:\", matches)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.504038Z","iopub.execute_input":"2023-11-03T12:04:34.505098Z","iopub.status.idle":"2023-11-03T12:04:34.515553Z","shell.execute_reply.started":"2023-11-03T12:04:34.505045Z","shell.execute_reply":"2023-11-03T12:04:34.514257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pattern = r'\\b@([A-Za-z0-9._]+)\\b'\nemails = \"\"\"\njohn.doe@example.com, jane_smith@gmail.com, info@Company.net, info@company.net\n\"\"\"\nmatches = re.findall(pattern, emails,  re.IGNORECASE)\nprint(\"Pattern 3 Matches:\", matches)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.517647Z","iopub.execute_input":"2023-11-03T12:04:34.518670Z","iopub.status.idle":"2023-11-03T12:04:34.530067Z","shell.execute_reply.started":"2023-11-03T12:04:34.518612Z","shell.execute_reply":"2023-11-03T12:04:34.528326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### But why is there an `r` before the pattern?<a class='anchor' id='whyr'></a> [↑](#top)\n\nThe `r` before the string in `r\"\\b[^@]+\\b\"` is called a \"raw string literal\" in Python. Let's explain why it's used:\n\nIn regular expressions, backslashes `\\` are commonly used to escape special characters. However, in Python strings, backslashes also have their own escape sequences. For instance, `\\n` represents a newline character, and `\\t` represents a tab character. This can lead to conflicts between the regular expression's use of backslashes and Python's string processing.\n\nTo address this, you can use a raw string literal by prefixing the string with `r`. In a raw string literal, backslashes are treated as literal characters and not as escape characters. This is particularly useful when working with regular expressions because it ensures that backslashes are interpreted only by the regex engine and not by Python's string handling.\n\nSo, the `r\"\\b[^@]+\\b\"` pattern uses the raw string literal `r` to make sure that the backslashes are interpreted as part of the regular expression and not as escape characters for Python's string processing.\n\n## Pattern 4: Matching URLs<a class='anchor' id='pattern4'></a> [↑](#top)\n\nPattern: \n```python\n'(https?://\\S+)'\n```\n### Explanation:\n\nThis pattern captures URLs starting with \"http://\" or \"https://\", followed by one or more non-whitespace characters. Let's break down the regular expression `(https?://\\S+)` step by step:\n\n1. `(https?://\\S+)` -  is the regular expression pattern enclosed in parentheses. The parentheses are used to create a capturing group that allows us to extract the matched content.\n\n2. `http` - `http` matches the literal characters \"http\" exactly.\n\n3. `s?` - `s?` is a quantifier that matches the character \"s\" zero or one time. This allows the regular expression to match both \"http\" and \"https\". The question mark `?` makes the preceding character (in this case, \"s\") optional.\n\n4. `://` - `://` matches the literal characters \"://\". This part represents the typical part of URLs that indicates the protocol (http or https).\n\n5. `\\S+`- `\\S+` matches one or more non-whitespace characters. It's used to match the rest of the URL after the protocol.\n\nBelow is a cheatsheet that might help you navigate the world of Regular Expressions\n\n<center>\n<img src=\"https://pbs.twimg.com/media/DBNN9XQXcAA8L-Q.jpg\" width=600>\n</center>","metadata":{}},{"cell_type":"code","source":"text = \"\"\"\n    Check this out: http://www.kaggle.com and also https://web.mit.edu/\n\"\"\"\n\npattern = r'(https?://\\S+)'\nmatches = re.findall(pattern, text)\nprint(\"\\nPattern 4 Matches:\", matches)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.532836Z","iopub.execute_input":"2023-11-03T12:04:34.534264Z","iopub.status.idle":"2023-11-03T12:04:34.545213Z","shell.execute_reply.started":"2023-11-03T12:04:34.534149Z","shell.execute_reply":"2023-11-03T12:04:34.543719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Pattern 5: Matching Dates<a class='anchor' id='pattern5'></a> [↑](#top)\nPattern: \n\n```python\n'(\\d{2}-\\d{2}-\\d{4})'\n```\n### Explanation:\n\nThis pattern captures dates in the format DD-MM-YYYY. Let's break down the regular expression `\\d{2}-\\d{2}-\\d{4}` step by step:\n\n1. `\\d{2}`- `\\d` matches any digit (0-9) and  `{2}` is a quantifier that specifies that the preceding element (in this case, `\\d`) should occur exactly 2 times while This part `\\d{2}` matches two consecutive digits.\n\n2. `-` - `-` matches the hyphen character \"-\" literally.\n\n3. `\\d{2}` - Similarly, this part `\\d{2}` matches two consecutive digits again.\n\n4. `-` - Another hyphen character \"-\" matches here.\n\n5. `\\d{4}` - This part `\\d{4}` matches four consecutive digits.","metadata":{}},{"cell_type":"code","source":"pattern = r'(\\d{2}-\\d{2}-\\d{4})'\ndates = \"\"\"\nImportant dates: 05-12-2022 and 2010-08-23\n\"\"\"\nmatches = re.findall(pattern, dates)\nprint(\"\\nPattern 5 Matches:\", matches)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.547654Z","iopub.execute_input":"2023-11-03T12:04:34.549009Z","iopub.status.idle":"2023-11-03T12:04:34.562189Z","shell.execute_reply.started":"2023-11-03T12:04:34.548954Z","shell.execute_reply":"2023-11-03T12:04:34.561017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n<p class=\"alert alert-block alert-info\">\nNotice that the second date is not matching since the pattern is not the same as we have mentioned. We can also write a more robust pattern for matching dates that handles different date formats, including variations with or without leading zeroes\n</p>\n\n### Explanation:\n\n- `\\b`: Word boundary to ensure complete date matches.\n- `\\d{1,2}`: Matches one or two digits for the day and month.\n- `[-/]`: Matches either a hyphen or a forward slash as the delimiter.\n- `\\d{1,2}`: Matches one or two digits for the day and month.\n- `[-/]`: Another delimiter.\n- `\\d{4}`: Matches exactly four digits for the year.\n- `\\b`: Word boundary to ensure the end of the date.\n\nThis pattern accommodates both single and double-digit days and months, as well as four-digit years, making it more robust for capturing various date formats. Below is the code for the above pattern we just went over:","metadata":{}},{"cell_type":"code","source":"pattern = r'\\b\\d{1,2}[-/]\\d{1,2}[-/]\\d{4}\\b'\ndates = \"\"\"\nImportant dates: 05-12-2022 and 2010-08-23 and 7/6/1998\n\"\"\"\nmatches5 = re.findall(pattern, dates)\nprint(\"Pattern 5 Matches:\", matches)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.564654Z","iopub.execute_input":"2023-11-03T12:04:34.565965Z","iopub.status.idle":"2023-11-03T12:04:34.576982Z","shell.execute_reply.started":"2023-11-03T12:04:34.565905Z","shell.execute_reply":"2023-11-03T12:04:34.575698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n<div class=\"alert alert-block alert-info\">\nNotice that the second date is not matching because the date pattern is different. The year is in front which doesn't allow the match condition to be true.\n</div>","metadata":{}},{"cell_type":"markdown","source":"## Pattern 6: Matching Any Character<a class='anchor' id='pattern6'></a> [↑](#top)\nPattern: \n```python\n'.*'\n```\n### Explanation:\n\n1. `.`: The dot (period) character in regex matches any character except for a newline character (`\\n`). It represents a wildcard that can stand for any single character.\n\n2. `*`: The asterisk is a quantifier that indicates \"zero or more occurrences\" of the preceding element. In this case, the preceding element is the dot (`.`), which means \"zero or more occurrences of any character.\"\n\n*For example*, if you have the text \"Hello, World!\", the pattern `.*` would match the entire string \"Hello, World!\". If used within a larger regex pattern, it would capture everything between other elements or patterns.","metadata":{}},{"cell_type":"code","source":"text = \"The quick brown fox jumps over the lazy dog\"\npattern = \"brown.*\"\nmatches = re.findall(pattern, text)\nprint(\"Pattern 6 Matches:\", matches)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.583279Z","iopub.execute_input":"2023-11-03T12:04:34.584114Z","iopub.status.idle":"2023-11-03T12:04:34.594840Z","shell.execute_reply.started":"2023-11-03T12:04:34.584072Z","shell.execute_reply":"2023-11-03T12:04:34.593574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Adding a word after our pattern `.*` will return all the characters upto that word starting from the beginning.","metadata":{}},{"cell_type":"code","source":"text = \"The quick brown fox jumps over the lazy dog\"\npattern = \".*brown\"\nmatches = re.findall(pattern, text)\nprint(\"Pattern 6 Matches:\", matches)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.596510Z","iopub.execute_input":"2023-11-03T12:04:34.596968Z","iopub.status.idle":"2023-11-03T12:04:34.611762Z","shell.execute_reply.started":"2023-11-03T12:04:34.596933Z","shell.execute_reply":"2023-11-03T12:04:34.610789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"And if you just want to match just a word you can just mention that word and that word will be returned to you. Let's do a list of sentences to see how we can loop through them to see if the word appears in those sentences or not","metadata":{}},{"cell_type":"code","source":"text = [\"The quick brown fox jumps over the lazy dog\", \"The lazy fox slept on the brown branch of a tree\"]\npattern = \"dog\"\nfor idx, match in enumerate(text):\n    matches = re.findall(pattern, match) \n    if matches:\n        print(f\"Pattern 6 Matches the input no. {idx+1}:\", matches[0])\n    else:\n        print(f\"Pattern 6 Match was not found in the input no. {idx+1}.\")","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:04:34.613896Z","iopub.execute_input":"2023-11-03T12:04:34.614982Z","iopub.status.idle":"2023-11-03T12:04:34.625840Z","shell.execute_reply.started":"2023-11-03T12:04:34.614931Z","shell.execute_reply":"2023-11-03T12:04:34.624544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Suppose you have an invoice number and you want to extract a specific part from the same. You could do that too.","metadata":{}},{"cell_type":"code","source":"text = \"TCS/2023-24/006\"\npattern = r\"\\d{3}$\"\nmatch = re.findall(pattern, text)[0]\nprint(f'The Pattern returned {match}')","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:06:56.569067Z","iopub.execute_input":"2023-11-03T12:06:56.569510Z","iopub.status.idle":"2023-11-03T12:06:56.577008Z","shell.execute_reply.started":"2023-11-03T12:06:56.569476Z","shell.execute_reply":"2023-11-03T12:06:56.575515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's say you want to get the year part from an invoice number. You could do it like this","metadata":{}},{"cell_type":"code","source":"text = \"TCS/2023-24/006\"\npattern = r\"\\d{4}\"\nmatch = re.findall(pattern, text)[0]\nprint(f'Year : {match}')","metadata":{"execution":{"iopub.status.busy":"2023-11-03T12:10:26.024210Z","iopub.execute_input":"2023-11-03T12:10:26.024712Z","iopub.status.idle":"2023-11-03T12:10:26.032501Z","shell.execute_reply.started":"2023-11-03T12:10:26.024667Z","shell.execute_reply":"2023-11-03T12:10:26.031087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Where to practice?<a class='anchor' id='practice'></a> [↑](#top)\n\nEnhance your mastery of regular expressions by utilizing platforms like https://regexr.com/ or https://regex101.com/ for hands-on practice. These websites offer an excellent environment to solidify your grasp of regular expressions. Once you've gained a solid foundation, you can further test your knowledge using Python or leverage the REGEXTRACT function in Google Sheets. These practical applications allow you to validate your learning and identify areas for improvement, if necessary.\n\nor you can practice using the code below.\n\n```python\n\nimport re\nimport ipywidgets as widgets\nfrom IPython.display import display\n\ntext_input = widgets.Text(\n    placeholder='Enter text here',\n    description='Text:',\n    disabled=False\n)\npattern_input = widgets.Text(\n    placeholder='Enter pattern here',\n    description='Pattern:',\n    disabled=False\n)\ndisplay(text_input, pattern_input)\n\n# Create a button widget\nbutton = widgets.Button(description=\"Find Matches\")\ndisplay(button)\n\n# Output widget to display matches\noutput = widgets.Output()\ndisplay(output)\n\n# Function to handle button click event\ndef find_matches(button_click):\n    text = text_input.value\n    pattern = pattern_input.value\n    matches = re.findall(pattern, text)\n    \n    with output:\n        print(\"Pattern Matches:\", matches)\n\n# Attach the function to the button's click event\nbutton.on_click(find_matches)\n```","metadata":{}},{"cell_type":"markdown","source":"## Conclusion<a class='anchor' id='conclusion'></a> [↑](#top)\n\n- Regular expressions serve as versatile tools that empower you to efficiently search for and manipulate text based on specific patterns. By grasping the fundamental concepts and patterns, you gain the ability to carry out diverse tasks involving text processing and data extraction. ## Conclusion\n\n- In our data-driven world, the capability to effectively process, search, and manipulate text data holds immense significance. Acquiring proficiency in regular expressions empowers you to unlock the full potential of text data and proficiently address a broad spectrum of challenges.\n\n- Whether you're a programmer, data analyst, content creator, or anyone engaging with text, mastering regular expressions significantly amplifies your productivity and ushers in fresh opportunities for innovative solutions.\n\n- While regular expressions can appear complex, particularly when unfamiliar, comprehending them offers you not only the capacity to create expressions tailored to your needs but also the ability to decipher expressions crafted by others.\n\n<div class=\"alert alert-block alert-warning\">\nI trust this notebook has provided a solid foundation in grasping the essentials of regex, leaving you confident in crafting your own expressions. Your support through upvoting and following for more content like this is greatly appreciated.\n</div>","metadata":{}}]}