Regular Expression in Python

Python : Regular Expression

Regular expressions (regex) are a powerful tool for pattern matching and string manipulation. They allow us to search for specific patterns in text data and perform various operations such as finding and replacing text, extracting specific parts of a string, and validating input data.

  • In Python, we can use the built-in re module to work with regular expressions. This module provides various functions and methods for working with regular expressions.

Basic Regular Expression Syntax:

Before diving into regular expression syntax, let's understand the basic symbols and characters used in regular expressions:

  • . - Matches any character except a newline character
  • ^ - Matches the start of a string
  • $ - Matches the end of a string
  • * - 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 any character in the square brackets
  • () - Groups expressions together
  • \ - Escapes special characters

Now, let's dive into the regular expression syntax and see some examples:

Matching a specific pattern:

Suppose we have a string and we want to find all occurrences of a specific pattern. We can use the search() function to search for the pattern in the string.

import re

string = "The quick brown fox jumps over the lazy dog"
pattern = "brown"

result = re.search(pattern, string)

print(result)
# Output: <re.Match object; span=(10, 15), match='brown'>

In the above example, we are searching for the word "brown" in the string. The search() function returns a match object that contains the starting and ending position of the matched string.

Matching a pattern with special characters:

Suppose we have a string that contains special characters and we want to match a specific pattern that includes those special characters. In such cases, we need to escape the special characters using the backslash (\) character.

import re

string = "The price of the product is $50"
pattern = "\$50"

result = re.search(pattern, string)

print(result)
# Output: <re.Match object; span=(22, 25), match='$50'>

In the above example, we are searching for the pattern $50 in the string. Since $ is a special character, we need to escape it using the backslash character.

Extracting a specific part of a string:

Suppose we have a string that contains some specific information that we want to extract. We can use regular expressions to extract the required information.

import re

string = "My email address is john@example.com"
pattern = "\w+@\w+\.\w+"

result = re.search(pattern, string)

print(result.group())
# Output: john@example.com

In the above example, we are searching for an email address in the string. The pattern \w+@\w+\.\w+ matches any word character (\w+) followed by the @ symbol, followed by any word character (\w+), followed by a dot (\.), and finally followed by any word character (\w+).

  • We use the group() function to extract the matched string.
  • In Python, we can use the built-in re module to work with regular expressions.
  • We can use regular expressions to search for specific patterns in text data, extract specific parts of a string

Serialization in Python

Code Introspection in Python

Previous Article

Next Article