Build regular expression patterns using metacharacters and quantifiers
Use re.search(), re.match(), re.findall(), and re.sub()
Capture groups to extract specific parts of text
Apply regex to validate emails, phone numbers, and log files
🔎 Regex Tester
Enter a pattern and test string to see matches highlighted. Try the example patterns below.
What Are Regular Expressions?
A regular expression (regex) is a pattern that describes a set of strings. Python's re module lets you search, match, and replace text using these patterns. Regex is used in form validation, log parsing, web scraping, and data cleaning.
Key Metacharacters
. matches any character except newline. \d matches a digit. \w matches a word character (letter, digit, underscore). \s matches whitespace.
* zero or more. + one or more. ? zero or one. {n,m} between n and m times.
^ start of string. $ end of string. [abc] character class. (group) capture group.
re Module Functions
re.search(pattern, string) — find pattern anywhere in string; returns Match or None
re.match(pattern, string) — match at the beginning only
re.findall(pattern, string) — return list of all matches
re.sub(pattern, replacement, string) — replace all matches
re.compile(pattern) — compile a pattern for reuse (faster in loops)
Quick Check
1. What does \d+ match?
Exactly one digit
One or more consecutive digits
Zero or more digits
A letter followed by a digit
2. What does re.findall(r'\d+', 'abc 123 def 456') return?
['123']
['123', '456']
('123', '456')
The first match object
3. What is the difference between re.match() and re.search()?
match() returns all matches; search() returns just the first
match() only checks at the beginning of the string; search() checks anywhere
search() is case-insensitive by default
They are identical in behavior
4. In the pattern r'(\w+)@(\w+)\.(\w+)', what do the parentheses create?
They have no effect — parentheses are ignored in Python regex
They make the group optional
Capture groups that can be extracted with .group(1), .group(2), etc.