WE CODE NOW
  • Home 
  • Blog 
  • Guides 
Guides
  1. Home
  2. Guides
  3. Python Programming
  4. Python Regular Expressions (Regex)

Python Regular Expressions (Regex)

Posted on June 1, 2024  (Last modified on June 8, 2024) • 2 min read • 258 words
Python
 
Regex
 
Text Processing
 
Pattern Matching
 
Python
 
Regex
 
Text Processing
 
Pattern Matching
 
Share via

Master regular expressions in Python for powerful text processing, including pattern matching, search, and replace operations.

On this page
  • Basics of Regex
    • Importing the re Module
    • Basic Pattern Matching
  • Search and Replace
    • Search
    • Replace
  • Groups and Capturing
    • Capturing Groups
    • Named Groups
  • Conclusion

Python Regular Expressions (Regex)  

Regular expressions (regex) are powerful tools for text processing. This guide covers pattern matching, search, and replace operations using Python’s re module.

Basics of Regex  

Importing the re Module  

import re

Basic Pattern Matching  

pattern = r"\bhello\b"
text = "hello world"

if re.search(pattern, text):
    print("Match found!")

Search and Replace  

Search  

Find all occurrences of a pattern.

pattern = r"\d+"
text = "There are 123 apples and 45 bananas."

matches = re.findall(pattern, text)
print(matches)  # Output: ['123', '45']

Replace  

Replace occurrences of a pattern.

pattern = r"\d+"
text = "There are 123 apples and 45 bananas."

new_text = re.sub(pattern, "number", text)
print(new_text)  # Output: There are number apples and number bananas.

Groups and Capturing  

Capturing Groups  

pattern = r"(\d+)\s(apples|bananas)"
text = "123 apples and 45 bananas"

matches = re.findall(pattern, text)
print(matches)  # Output: [('123', 'apples'), ('45', 'bananas')]

Groups allow you to extract specific parts of the match.

match = re.search(pattern, text)
if match:
    quantity = match.group(1)
    fruit = match.group(2)
    print(f"Quantity: {quantity}, Fruit: {fruit}")  # Output: Quantity: 123, Fruit: apples

Named Groups  

pattern = r"(?P<quantity>\d+)\s(?P<fruit>apples|bananas)"
text = "123 apples and 45 bananas"

for match in re.finditer(pattern, text):
    print(f"Quantity: {match.group('quantity')}, Fruit: {match.group('fruit')}")

Named groups provide a way to access captured groups by name.

match = re.search(pattern, text)
if match:
    quantity = match.group('quantity')
    fruit = match.group('fruit')
    print(f"Quantity: {quantity}, Fruit: {fruit}")

Conclusion  

Mastering regular expressions can greatly enhance your text processing capabilities in Python. Practice using regex for pattern matching, search, and replace operations to handle complex text manipulation tasks.

 Utilizing Python Modules and Packages
Handling Dates and Times in Python 
On this page:
  • Basics of Regex
    • Importing the re Module
    • Basic Pattern Matching
  • Search and Replace
    • Search
    • Replace
  • Groups and Capturing
    • Capturing Groups
    • Named Groups
  • Conclusion
Copyright © 2024 WE CODE NOW All rights reserved.
WE CODE NOW
Link copied to clipboard
WE CODE NOW
Code copied to clipboard