Python RegEx

Regular expressions, often abbreviated as regex, are a powerful tool for matching patterns in strings. Python’s re module provides a set of functions that allows you to search a string for a match using a regular expression. This lesson will introduce you to the basics of using regular expressions in Python.


What are Regular Expressions?

Regular expressions are sequences of characters that form search patterns. They can be used to check if a string contains a specific pattern, extract parts of the string, and replace parts of the string with other text.




The re Module

To work with regular expressions in Python, you need to import the re module:

import re




Common Functions in re

  • re.match(): Determines if the RE matches at the beginning of the string.
  • re.search(): Scans through a string, looking for any location where this RE matches.
  • re.findall(): Finds all the matches and returns them as a list.
  • re.sub(): Replaces occurrences of a pattern found in the string.




Regular Expression Patterns

  • Literal characters: Simply match themselves. Example: abc matches ‘abc’.
  • Dot (.): Matches any character except a newline.
  • Character classes ([]): Matches any single character within the brackets. Example: [a-z] matches any lowercase letter.
  • Quantifiers (*, +, ?, {}): Specify the number of occurrences to match.
  • * matches 0 or more occurrences.
  • + matches 1 or more occurrences.
  • ? matches 0 or 1 occurrence.
  • {m,n} matches between m and n occurrences.




Python RegEx Examples

Example 1: Matching a Phone Number

import re

pattern = r'\d{3}-\d{2}-\d{4}'
text = 'My phone number is 123-45-6789'

match = re.match(pattern, text)
if match:
    print("Match found!")
else:
    print("No match found.")

Example 2: Finding All Numbers

import re

pattern = r'\d+'
text = 'There are 123 apples and 45 oranges'

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

Example 3: Replacing White Spaces

import re

pattern = r'\s'
replacement = '-'
text = 'Hello World'

new_text = re.sub(pattern, replacement, text)
print(new_text)  # Output: 'Hello-World'




Python Labs

Scroll to Top