Regular Expressions (regex) are patterns used to match character combinations in strings. Let’s go through some examples using Python’s re module.
-
Importing the
remodule:import re
-
Basic Match:
pattern = r"hello" text = "hello world" match = re.search(pattern, text) if match: print("Found:", match.group()) -
.: Matches any character except newline.^: Matches the start of the string.$: Matches the end of the string.*: Matches 0 or more repetitions of the preceding character.+: Matches 1 or more repetitions of the preceding character.?: Matches 0 or 1 repetition of the preceding character.{m,n}: Matches frommtonrepetitions of the preceding character.
Using Metacharacters:
pattern = r"he..o" text = "hello world" match = re.search(pattern, text) if match: print("Found:", match.group()) -
\d: Matches any digit (equivalent to[0-9]).\D: Matches any non-digit.\w: Matches any word character (equivalent to[a-zA-Z0-9_]).\W: Matches any non-word character.\s: Matches any whitespace character.\S: Matches any non-whitespace character.
Character Classes:
pattern = r"\d+" text = "There are 42 apples" match = re.search(pattern, text) if match: print("Found:", match.group()) -
Grouping and Capturing:
pattern = r"(hello) (world)" text = "hello world" match = re.search(pattern, text) if match: print("Found:", match.group()) print("Group 1:", match.group(1)) print("Group 2:", match.group(2)) -
Finding All Matches:
pattern = r"\d+" text = "There are 42 apples and 35 oranges" matches = re.findall(pattern, text) print("All Matches:", matches) -
Replacing Text:
pattern = r"apples" replacement = "bananas" text = "I have apples" new_text = re.sub(pattern, replacement, text) print("New Text:", new_text) -
Splitting Strings:
pattern = r"\s+" text = "This is a test" split_text = re.split(pattern, text) print("Split Text:", split_text)