Zone Of Makos

Menu icon

Regex in Python

Regular expressions (regex) are a powerful tool for working with text in Python. Regex allows you to search for patterns in a string, and then manipulate or extract those patterns as needed. In this lesson, we'll explore how to use regex in Python, including basic syntax, search patterns, and commonly used functions and methods.

Basic Syntax

The basic syntax for regex in Python is to create a regular expression pattern, and then use a regex function to search for that pattern in a string. In Python, the most commonly used regex functions are part of the "re" module.


import re

# Create a regex pattern to match a string that starts with "Hello"
pattern = "^Hello"

# Use the regex search function to find matches in a string
match = re.search(pattern, "Hello World")

# The match object contains information about the match
print(match.group()) # Output: "Hello"

Search Patterns

Regex patterns can be as simple or as complex as you need them to be. Some common search patterns include matching a specific character or set of characters, matching any character, matching a repeated character or set of characters, and matching a specific position within a string.

You can use regular expressions to search for more complex patterns as well. Here are some common regex patterns:

  • ' . ' matches any character except a newline.
  • ' * ' matches zero or more occurrences of the preceding character.
  • ' + ' matches one or more occurrences of the preceding character.
  • ' ? ' matches zero or one occurrence of the preceding character.
  • ' ^ ' matches the beginning of a string.
  • ' $ ' matches the end of a string.
  • ' [] ' matches any single character in the brackets.
  • ' () ' groups patterns together.

Here's an example that uses some of these patterns:


import re

text = "The quick brown fox jumps over the lazy dog"
pattern = "^The.*dog$"

match = re.search(pattern, text)

if match:
    print("Match found!")
else:
    print("Match not found.")

Commonly Used Functions and Methods

In addition to the search function, the "re" module also provides a number of other functions and methods for working with regex patterns in Python. Some commonly used functions and methods include "match" (which searches for a pattern at the beginning of a string), "findall" (which returns a list of all matches in a string), and "sub" (which replaces matches with a specified string).


import re

# Use the match function to search for a pattern at the beginning of a string
match = re.match("^Hello", "Hello World")

# The match object contains information about the match
print(match.group()) # Output: "Hello"

# Use the findall function to find all matches in a string
matches = re.findall("cat|dog", "I have a cat and a dog")

# The matches list contains all matches
print(matches) # Output: ["cat", "dog"]

# Use the sub function to replace matches with a specified string
new_string = re.sub("cat", "bird", "I have a cat and a dog")

# The new string contains the replaced matches
print(new_string) # Output: "I have a bird and a dog"

Conclusion

Regex can be a powerful tool for working with text data in Python. Understanding how to use it effectively can make your code more efficient and robust.