Regex in Python

Introduction

  • Python has re library to work with regular expressions.
  • Inbuilt Library i.e. No need to install anything extra.
  • Provide functions to search, validate, replace patterns

search

  • Searches pattern within a given text.
  • Returns first substring that matches provided pattern or None.
  • .group method is used to print matched text
  • Example
    1. import re usr_txt = 'I bought 12 kg of appples. weight_regex=r'\d+(?= kg)' match = re.search(weight_regex, usr_txt) if match: print("Weight:", match.group()) # .start, .end, .span else: print("No weight found.")

Named Group

  • Use (?P<name>...) syntax to assign a name to a capturing group.
  • Allows us to reference matched text by name instead of number.
  • Example
    1. usr_txt = "Date of today is 2026-07-10. I have 3 class today." date_regex = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})' match = re.search(date_regex, usr_txt) if match: print("Year:", match.group("year")) print("Month:", match.group("month")) print("Day:", match.group("day")) else: print("No date found.")

findall

  • Returns a list of all non-overlapping matches of pattern in string.
  • Returns list of matched strings.
  • Example
    1. usr_txt = "I bought 10 kg apple, 5 kg mango and 12 litre curd for salad." weight_regex = r'\d+(?= kg)' weights = re.findall(weight_regex, usr_txt) print("Weights found:", weights)

split

  • Splits string by occurrences of pattern.
  • Returns list of substrings.
  • Example
    1. usr_txt = "I bought 10 kg apple, 5 kg mango and 12 litre curd for salad." weight_regex = r'\d+ kg' parts = re.split(weight_regex, usr_txt) print("Parts after splitting:", parts)

sub

  • Replaces all occurrences of pattern in string with replacement.
  • Returns the modified string.
  • Example
    1. usr_txt = "I bought 10 kg apple, 5 kg mango and 12 litre curd for salad." weight_regex = r'\d+ kg' replacement = "X kg" new_txt = re.sub(weight_regex, replacement, usr_txt) print("Modified text:", new_txt)