Regex in Python
✕Introduction
- Python has
relibrary 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. .groupmethod is used to print matched textimport 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,.spanelse: print("No weight found.")
Example
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.
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.")
Example
findall
- Returns a list of all non-overlapping matches of pattern in string.
- Returns list of matched strings.
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)
Example
split
- Splits string by occurrences of pattern.
- Returns list of substrings.
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)
Example
sub
- Replaces all occurrences of pattern in string with replacement.
- Returns the modified string.
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)
Example
