Regular Expressions in Python: Complete Guide with Examples

Regular Expressions in Python: Complete Guide with Examples

Regular Expressions in Python: Complete Guide with Examples

Regular Expressions (RegEx) are a powerful tool in Python for pattern matching and text processing. Whether you’re validating user input, searching for patterns in strings, or manipulating text, RegEx simplifies complex operations efficiently.

Why Use Regular Expressions?

Regular expressions allow you to search for specific patterns in strings. For example, suppose you need to find employees whose names:

  • Start with ‘h’
  • End with ‘i’
  • Have a total length of six characters

Using RegEx, the pattern ^h....i$ helps identify such names.

Explanation of the Pattern:

  • ^ Ensures the string starts with ‘h’.
  • .... Represents any four characters.
  • i$ Ensures the string ends with ‘i’.

Example Matches:

NameMatches?
hariniYes
henriNo

Implementing RegEx in Python

Python provides the re module to work with regular expressions. The primary method for pattern matching is match().

Example: Matching a Pattern

import re

pattern = '^h....i$'
string = 'harini'

result = re.match(pattern, string)

if result:
    print("Pattern Matched")
else:
    print("Pattern Not Matched")

Output:

Pattern Matched

Common RegEx Metacharacters

Metacharacters define the matching rules. Below are some essential metacharacters:

SymbolDescriptionExample
.Matches any character except newlineh.llo → matches hello
^Matches the start of the string^hello → matches hello world
$Matches the end of the stringworld$ → matches hello world
*Matches 0 or more repetitionsa* → matches aaa
+Matches 1 or more repetitionsa+ → matches aaa
[]Matches any character inside brackets[abc] → matches a, b, or c

Special Sequences in RegEx

Special sequences simplify pattern matching:

SequenceDescriptionExample
\dMatches any digit (0-9)\d+ → matches 123
\wMatches alphanumeric characters\w+ → matches hello123
\sMatches any whitespace character\s → matches spaces or tabs
\bMatches word boundaries\bword\b → matches word

Essential RegEx Methods in Python

Python’s re module provides useful methods:

1. findall() – Find All Matches

import re

result = re.findall('[AFP]', 'FACE Prep')
print(result)

Output: ['F', 'A', 'P']

2. split() – Split String by a Pattern

result = re.split('\d+', 'FACE10Prep3Python')
print(result)

Output: ['FACE', 'Prep', 'Python']

3. search() – Find First Match

result = re.search('FA', 'FACE Prep')
if result:
    print("Pattern Found")
else:
    print("Pattern Not Found")

Output: Pattern Found

Frequently Asked Questions (FAQs)

1. What is a Regular Expression in Python?

A regular expression is a sequence of characters that define a search pattern, primarily used for string matching and text processing.

2. How to check if ‘m’ appears more than once in a string?

Use m* (matches zero or more) or m+ (matches one or more occurrences).

result = re.search('m+', 'programming')
if result:
    print("Found")

3. How to check if ‘!’ appears exactly once in a string?

Use the pattern !?.

result = re.search('!?', 'Hello World!')
if result:
    print("Found")
else:
    print("Not Found")

Conclusion

Regular expressions in Python are a powerful tool for pattern matching and text processing. With the re module, you can efficiently search, replace, and manipulate strings using metacharacters, special sequences, and built-in functions like match(), findall(), and search(). Whether you’re validating input, extracting data, or parsing text, mastering RegEx can significantly improve your coding efficiency.

Regular Expressions in Python: Complete Guide with Examples