What is RegEx in Python with example?

You can use the re module to work with regular expressions. Here’s an example that demonstrates the basic usage of regular expressions in Python:

import re

# Example string
text = "Hello, my phone number is 123-456-7890. Please call me!"

# Define a regular expression pattern
pattern = r'\d{3}-\d{3}-\d{4}'

# Use the pattern to find matches in the text
matches = re.findall(pattern, text)

# Print the matches
for match in matches:
    print("Phone number:", match)
Code language: Python (python)

Output:

Phone number: 123-456-7890Code language: Python (python)

In the example above, we import the re module and define a regular expression pattern r'\d{3}-\d{3}-\d{4}', which matches the pattern of a phone number in the format ###-###-####, where each # represents a digit.

We then use the re.findall() function to find all occurrences of the pattern in the given text. It returns a list of all matching substrings.

Finally, we iterate over the matches list and print each phone number that was found in the text.

Note that the r prefix before the pattern string denotes a raw string in Python, which is used to treat backslashes (\) as literal characters. This is important when working with regular expressions to avoid any unintended escape sequences.

How do you create a regular expression in Python?

To create a regular expression in Python, you need to use the re module. Regular expressions are represented as strings and are enclosed between forward slashes (/) or are defined as raw strings using the r prefix.

Here are some common elements and patterns you can use to create regular expressions in Python:

  1. Literal Characters: Literal characters in a regular expression match themselves. For example, the regular expression cat matches the string “cat” exactly.
  2. Character Classes: Character classes allow you to match any one character from a set of characters. Some common character classes include:
    • [abc]: Matches any character that is either ‘a’, ‘b’, or ‘c’.
    • [a-z]: Matches any lowercase letter from ‘a’ to ‘z’.
    • [0-9]: Matches any digit from 0 to 9.
    • [^abc]: Matches any character except ‘a’, ‘b’, or ‘c’.
  3. Quantifiers: Quantifiers specify how many times a particular element should occur. Some common quantifiers include:
    • *: Matches zero or more occurrences of the previous element.
    • +: Matches one or more occurrences of the previous element.
    • ?: Matches zero or one occurrence of the previous element.
    • {n}: Matches exactly ‘n’ occurrences of the previous element.
    • {n, m}: Matches between ‘n’ and ‘m’ occurrences of the previous element.
  4. Anchors: Anchors are used to specify the position of a pattern in a string. Some common anchors include:
    • ^: Matches the start of a string.
    • $: Matches the end of a string.
    • \b: Matches a word boundary.
  5. Escape Sequences: Some characters have special meanings in regular expressions, such as . or *. To match these characters literally, you need to escape them using a backslash (\).

Here’s an example that combines some of these elements to create a regular expression pattern that matches a valid email address:

import re

pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'

email = "[email protected]"

if re.match(pattern, email):
    print("Valid email address.")
else:
    print("Invalid email address.")Code language: Python (python)

Output:

Valid email address.Code language: Python (python)

In the example above, the regular expression pattern r'^[\w\.-]+@[\w\.-]+\.\w+$' matches a valid email address. Let’s break it down:

  • ^: Matches the start of the string.
  • [\w\.-]+: Matches one or more word characters, dots, or dashes (the username part of the email address).
  • @: Matches the ‘@’ symbol.
  • [\w\.-]+: Matches one or more word characters, dots, or dashes (the domain name part of the email address).
  • \.: Matches a dot.
  • \w+: Matches one or more word characters (the top-level domain part of the email address).
  • $: Matches the end of the string.

The re.match() function is used to determine if the given email matches the pattern. If it does, the email is considered valid; otherwise, it is considered invalid.

Regular expressions can be complex and powerful, allowing you to match and manipulate strings with great flexibility. The examples above cover only the basics, but you can explore more advanced regular expression patterns and techniques depending on your specific needs.

Read More;

    by
  • A Z Hasnain Kabir

    I am an aspiring software engineer currently studying at the Islamic University of Technology in Bangladesh. My technical skills include Python automation, data science, machine learning, PHP, cURL and more. With a passion for creating innovative solutions, I am dedicated to mastering the art of software engineering and contributing to the technological advancements of tomorrow.

Leave a Comment