Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Mastering Regular Expressions in Python

July 18, 2024

Mastering Regular Expressions in Python

Regular expressions are powerful tools for pattern matching and text manipulation. They can be used in various programming languages, including Python. In this blog post, we will explore the basics of regular expressions in Python and provide extensive examples and explanations to help you master this valuable skill.

What are Regular Expressions?

Regular expressions, commonly referred to as regex, are sequences of characters that define a search pattern. They are used to match and manipulate text based on specific patterns.

Python provides the re module for working with regular expressions. To use regular expressions in Python, you need to import this module:

1
import re

Basic Regular Expression Patterns

Regular expressions consist of various characters and metacharacters that represent specific patterns. Here are some basic patterns used in regular expressions:

Using Regular Expressions in Python

To apply regular expressions in Python, you need to use the methods provided by the re module. The most commonly used methods are re.search() and re.findall().

The re.search(pattern, string) method searches for a pattern in a string and returns a match object if found. Here is an example:

1
2
3
4
5
6
7
import re

string = "The cat is black."
pattern = "cat"

match = re.search(pattern, string)
print(match)

In this example, the re.search() method searches for the pattern “cat” in the string “The cat is black.” The match object is then printed. If a match is found, it will display information about the match; otherwise, it will return None.

The re.findall(pattern, string) method returns all non-overlapping occurrences of a pattern in a string. For example:

1
2
3
4
5
6
7
import re

string = "The cat and the hat."
pattern = "at"

matches = re.findall(pattern, string)
print(matches)

In this case, the re.findall() method looks for all occurrences of the pattern “at” in the string “The cat and the hat.” The matches are returned as a list and printed.

Advanced Examples

Let’s dive into some advanced examples to explore the capabilities of regular expressions in Python.

Matching Phone Numbers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import re

phone_numbers = [
    "123-456-7890",
    "555-1234",
    "1-800-555-5555",
]

pattern = r"\d{3}-\d{3}-\d{4}"

for number in phone_numbers:
    match = re.search(pattern, number)
    if match:
        print(f"Valid phone number: {match.group()}")
    else:
        print(f"Invalid phone number: {number}")

In this example, we define a regular expression pattern to match phone numbers in the format “xxx-xxx-xxxx” (where “x” represents a digit). We use re.search() to find the pattern in each phone number, and if a match is found, we print it; otherwise, we print that it’s invalid.

Extracting Email Domains

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import re

emails = [
    "example1@gmail.com",
    "example2@yahoo.com",
    "example3@hotmail.com",
]

pattern = r"@(\w+\.\w+)"

for email in emails:
    match = re.search(pattern, email)
    if match:
        domain = match.group(1)
        print(f"Domain of {email} is {domain}")

In this example, we want to extract the domains from a list of email addresses. The regular expression pattern @(\w+\.\w+) matches the domain after the “@” symbol. The group(1) method returns the matched pattern within parentheses, which represents the domain. We then print the email and its corresponding domain.

Conclusion

Regular expressions are a powerful tool for pattern matching and text manipulation in Python. In this blog post, we explored the basics of regular expressions and provided extensive examples and explanations to help you master this skill. By applying regular expressions effectively, you can enhance the functionality of your Python programs and simplify complex string operations. Happy coding!


➡️ Programming ATmega-328 using Arduino IDE


⬅️ Getting Started with ATmega-328 Development


Go back to Posts.