top of page

Python Practice Set (Part 16): Mastering Regular Expressions (Regex)

ree

The Ultimate Text-Processing Tool: A Guide to Python's Regular Expressions


Welcome to Part 16 of our Python coding series! Have you ever needed to find a specific pattern in a large block of text, not just a fixed word? This is where Regular Expressions, or "Regex," come in. Regex is a special sequence of characters that defines a search pattern, acting like a super-powered "find and replace." It's an indispensable tool for data validation, web scraping, and data cleaning. In this set, we'll explore Python's built-in re module to unlock this power.


1. The Simple Search: Finding a Word


Let's start with the basics. You have a sentence: "Python is a powerful and versatile programming language.". Write a function contains_word that takes a text and a word as input. The function should use the re.search()method to check if the given word exists anywhere in the text and should return True if a match is found, otherwise False.


2. Find All Numbers in a String


You have a string containing mixed text and numbers: "The order IDs are 456, 7890, and 123.". Your task is to extract all the numerical values from this string into a list. Use the re.findall() method with a pattern that matches one or more digits.

Hint: The special character \d matches any digit (0-9).


3. Extracting an Email Address with Capture Groups


Regular expressions are excellent for extracting structured data. Given an email address string like "contact.user@example.com", write a function extract_email_parts that uses a regex with capture groups () to extract the "username" (contact.user) and the "domain" (example.com). The function should return these two parts as a tuple.


4. Redacting Phone Numbers with Substitution


You need to censor sensitive information in a text. You have the string: "Please contact support at 555-123-4567 or the main office at 555-987-6543.". Use the re.sub() method to find all patterns that look like a phone number (XXX-XXX-XXXX) and replace them with the string "[REDACTED]".


5. Validating a Date Format (Anchors and Quantifiers)


Write a function is_valid_date that checks if a given string is in the YYYY-MM-DD format. The function should return True for valid formats like "2025-09-03" but False for invalid ones like "03-09-2025" or "2025/09/03".

Hint: Use anchors ^ (start of string) and $ (end of string) to ensure the whole string matches. Use quantifiers {n} to specify the exact number of digits.




Comments


bottom of page