Python RegEx (Regular Expression)

Updated: April 19th, 2023, 08:28:18 IST
Published: April 19th, 2023
Python RegEx (Regular Expression)
Title: Python RegEx (Regular Expression)

Python RegEx (Regular Expression) is a powerful tool used for pattern matching and string manipulation. It provides a way to search and manipulate text by matching patterns within the text. Python's re module is used to work with regular expressions.

In this blog post, we will cover the basics of regular expressions in Python, including syntax, meta-characters, quantifiers, groups, and more.

Regular Expression Syntax

A regular expression is a sequence of characters that define a search pattern. The syntax of regular expressions is based on special characters and symbols that represent specific patterns. Some of the basic meta-characters in Python regular expressions include:

  • . : matches any single character except newline.
  • ^ : matches the start of a string.
  • $ : matches the end of a string.
  • [] : matches any character within the brackets.
  • [^] : matches any character that is not in the brackets.
  • | : matches either the left or right expression.
  • () : groups expressions together.

Using Regular Expressions in Python

To use regular expressions in Python, we need to import the re module. The re module provides several methods for working with regular expressions, including:

  • re.search(pattern, string) : searches the string for a match to the specified pattern and returns a match object if there is a match.
  • re.match(pattern, string) : checks if the pattern matches at the beginning of the string and returns a match object if there is a match.
  • re.findall(pattern, string) : returns all non-overlapping matches of the pattern in the string as a list of strings.
  • re.sub(pattern, repl, string) : replaces all occurrences of the pattern in the string with the replacement string repl.
  • re.compile(pattern) : compiles the pattern into a regular expression object that can be used multiple times for searching or matching.

Examples

Matching a Pattern

Let's start with a simple example of searching for a specific pattern within a string. We will use the re.search() method to search for the pattern "world" within the string "hello world":

import re

string = "hello world"
pattern = "world"

match = re.search(pattern, string)

if match:
    print("Pattern found in string.")
else:
    print("Pattern not found in string.")

Output:

Pattern found in string.

Matching Multiple Patterns

We can use square brackets [] to match any one character within a specified set of characters. For example, to match any vowel in a string, we can use the following pattern:

import re

string = "The quick brown fox jumps over the lazy dog."
pattern = "[aeiou]"

matches = re.findall(pattern, string)

print(matches)

Output:

['e', 'u', 'i', 'o', 'o', 'u', 'o', 'e', 'a', 'o']

Replacing a Pattern

We can use the re.sub() method to replace all occurrences of a pattern in a string with a specified replacement string. For example, to replace all occurrences of the word "apple" in a string with "orange", we can use the following code:

import re

string = "I have an apple, she has an apple, and he has an apple too."
pattern = "apple"
replacement = "orange"

new_string = re.sub(pattern, replacement, string)

print(new_string)

Output:

I have an orange, she has an orange, and he has an orange too.

Conclusion

In this blog post, we covered the basics of Python regular expressions, including syntax, meta-characters, quantifiers, groups, and more. We also provided examples of how to use regular expressions for pattern matching and string manipulation in Python. With this knowledge, you can start using regular expressions to manipulate and extract data from text in your Python projects.