08-1 Python Course – Exploring Regular Expressions

What is Regular Expression?

Regular Expressions (regex or regexp for short) are strings used for searching, replacing, and extracting strings that match specific rules. They are mainly used in text processing to search multiple patterns or for data validation.

Basic Concepts of Regular Expressions

To understand the basic concepts of regular expressions, it is essential to know a few special characters that are commonly used.

Basic Patterns

  • Dot (.): Represents any single character.
  • Brackets ([]): Represents one of the characters inside the brackets. Example: [abc]
  • Caret (^): Represents the start of the string. Example: ^Hello
  • Dollar sign ($): Represents the end of the string. Example: world$
  • Asterisk (*): Indicates that the preceding character may occur zero or more times. Example: a*
  • Plus (+): Indicates that the preceding character may occur one or more times. Example: a+
  • Question mark (?): Indicates that the preceding character may occur zero or one time. Example: a?

Meta Characters

Meta characters are used with special meanings in regular expressions and often need to be escaped to be used literally as characters.

  • Backslash (\\): An escape character used to treat a special character as a regular character.
  • Pipe (|): The OR operator, which is considered true if any of the multiple patterns match. Example: a|b
  • Parentheses ((): Represents grouping and is used to create subpatterns. Example: (ab)

Using Regular Expressions in Python

In Python, the `re` module is used to handle regular expressions. This module provides various functions to easily work with regular expressions.

Functions in the re Module

  • re.match(): Checks if the beginning of a string matches the specified pattern.
  • re.search(): Searches the entire string for the first matching pattern.
  • re.findall(): Returns all substrings that match the pattern as a list.
  • re.finditer(): Returns all substrings that match the pattern as an iterable object.
  • re.sub(): Replaces substrings that match the pattern with another string.

Examples of Using Regular Expressions

Basic Usage Examples


import re

# Check if the start of the string is 'Hello'
result = re.match(r'^Hello', 'Hello, world!')
print(result)  # Returns a match object if successful, or None if failed.
    

Finding Patterns in a String


import re

search_result = re.search(r'world', 'Hello, world!')
print(search_result)  # Returns a match object for the matched portion.
    

Extracting All Matching Patterns


# Finding all 'a' characters in the string
all_matches = re.findall(r'a', 'banana')
print(all_matches)  # Returns a list of all matches found.
    

Transforming Strings Based on Patterns

You can use the re.sub() function to transform patterns in a string into other strings.


# Replace all whitespace with underscores
transformed_string = re.sub(r'\s', '_', 'Hello world!')
print(transformed_string)  # Output: 'Hello_world!'
    

Advanced Features of Regular Expressions

Grouping and Capturing

Grouping is very useful for capturing subpatterns of a regex for reuse or for performing specific tasks.


pattern = r'(\d+)-(\d+)-(\d+)'
string = 'Phone number: 123-456-7890'
match = re.search(pattern, string)

if match:
    print(match.group(0))  # Full matched string
    print(match.group(1))  # First group: 123
    print(match.group(2))  # Second group: 456
    print(match.group(3))  # Third group: 789
    

Lookahead and Lookbehind

Lookahead and Lookbehind are used to check conditions that are before or after a specific pattern. These features are commonly used techniques but can be somewhat complex.

Using Lookahead


# Finding the pattern where 'def' follows 'abc'
lookahead_pattern = r'abc(?=def)'
lookahead_string = 'abcdefghi'
lookahead_match = re.search(lookahead_pattern, lookahead_string)
print(lookahead_match)
    

Using Lookbehind


# Pattern that comes before '123'
lookbehind_pattern = r'(?<=123)abc'
lookbehind_string = '123abc'
lookbehind_match = re.search(lookbehind_pattern, lookbehind_string)
print(lookbehind_match)
    

Comprehensive Example: Extracting Email Addresses

Regular expressions are especially useful for extracting email addresses from entered text.


email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
text = "Contact email: example@example.com or support@domain.com"
emails = re.findall(email_pattern, text)

print(emails)  # ['example@example.com', 'support@domain.com']
    

Summary

Regular expressions are a powerful tool in string processing, and Python's `re` module provides sufficient functionality to work with them. By understanding the basic syntax of regular expressions and practicing, one can easily handle complex text patterns. Regular practice and application of these techniques will help solve more complex string processing issues effectively.