Regex Tester — Developer Code Samples
Regular expressions are powerful pattern-matching tools built into every major language. Python's re module, JavaScript's RegExp, and grep/sed provide regex support with slightly different syntax. These examples cover common use cases including validation, extraction, and substitution.
Try the interactive version online:
Open Regex Tester Tool →
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| pattern | str | Yes | Regular expression pattern string |
| text | str | Yes | Input text to test the pattern against |
| flags | str | No | Regex flags: i (ignore case), m (multiline), s (dotall) |
Returns: List of matches (findall), single match object (match/search), or substituted string (sub)
Code Examples
import re
# Test if a pattern matches
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
email = "[email protected]"
is_match = bool(re.match(pattern, email))
print(f"Valid email: {is_match}") # True
# Find all matches
text = "Prices: $10.99, $5.50, $100.00"
prices = re.findall(r'\$([\d.]+)', text)
print(prices) # ['10.99', '5.50', '100.00']
# Extract named capture groups
log_pattern = r'(?P<date>\d{4}-\d{2}-\d{2}) (?P<level>\w+) (?P<message>.+)'
log_line = "2024-01-15 ERROR Database connection failed"
m = re.match(log_pattern, log_line)
if m:
print(m.group('date')) # 2024-01-15
print(m.group('level')) # ERROR
print(m.group('message')) # Database connection failed
# Substitution
text = "Hello World Foo"
cleaned = re.sub(r'\s+', ' ', text)
print(cleaned) # Hello World Foo
# Case-insensitive search
hits = re.findall(r'python', "I use Python and python", re.IGNORECASE)
print(hits) # ['Python', 'python']
# Split on multiple delimiters
parts = re.split(r'[,;|]', "one,two;three|four")
print(parts) # ['one', 'two', 'three', 'four']