Regular Expressions Examples

Example 1: Match a word

import re
pattern = r"apple"
text = "I ate an apple."
print(re.search(pattern, text))

Example 2: Match digits

pattern = r"\d+"
text = "There are 24 apples"
print(re.findall(pattern, text))

Example 3: Match email address

pattern = r"[\w.-]+@[\w.-]+\.\w+"
text = "Contact us at info@example.com"
print(re.search(pattern, text))

Example 4: Validate phone number

pattern = r"\d{10}"
text = "My number is 9876543210"
print(re.search(pattern, text))

Example 5: Match words starting with 'a'

pattern = r"\ba\w*"
text = "apple and banana"
print(re.findall(pattern, text))

Example 6: Extract domain from URL

pattern = r"https?://(\w+\.\w+)"
text = "Visit https://example.com for more"
print(re.search(pattern, text).group(1))

Example 7: Replace digits with '#' symbol

pattern = r"\d"
text = "Phone: 12345"
print(re.sub(pattern, "#", text))

Example 8: Match repeated characters

pattern = r"a{2,4}"
text = "aaa aa aaaaa"
print(re.findall(pattern, text))

Example 9: Ignore case while matching

pattern = r"python"
text = "I love Python"
print(re.search(pattern, text, re.IGNORECASE))

Example 10: Match optional character

pattern = r"colou?r"
text = "color and colour are both accepted"
print(re.findall(pattern, text))

Example 11: Match a specific pattern

pattern = r"\b\d{3}-\d{2}-\d{4}\b"
text = "My SSN is 123-45-6789"
    print(re.search(pattern, text))

Example 12: Match a URL

pattern = r"https?://[^\s]+"
text = "Visit https://www.example.com for more info"
    print(re.search(pattern, text))

example 13 : named groups

pattern = r"(?P<first>\w+) (?P<last>\w+)"
text = "John Doe"
match = re.search(pattern, text)
if match:
    print(f"First Name: {match.group('first')}, Last Name: {match.group('last')}")

Example 14 : Named capturing groups

pattern = r"(?P<name>\w+) (?P<age>\d+)"
text = "Alice 30"   
match = re.search(pattern, text)
if match:
    print(f"Name: {match.group('name')}, Age: {match.group('age')}")

Example 15 : Positive lookahead assertion

pattern = r"\w+(?=\d)"
text = "abc123 def456 ghi789"
    print(re.findall(pattern, text))

Example 16 : Negative lookahead assertion

pattern = r"\w+(?!\d)"
text = "abc123 def456 ghi789"
    print(re.findall(pattern, text))

Example 17 : Positive lookbehind assertion

pattern = r"(?<=\d{3})\w+"
text = "123abc 456def 789ghi"
    print(re.findall(pattern, text))

Example 18 : Negative lookbehind assertion

pattern = r"(?<!\d{3})\w+"
text = "123abc 456def 789ghi"
    print(re.findall(pattern, text))

Example 19 : Verbose mode

pattern = r"""(?P<name>\w+)  # Match a name
(?P<age>\d+)  # Match an age
"""
text = "Alice 30"   
match = re.search(pattern, text, re.VERBOSE)
if match:
    print(f"Name: {match.group('name')}, Age: {match.group('age')}")

Example 20 : Using flags

pattern = r"python"
text = "I love Python"
print(re.search(pattern, text, re.IGNORECASE))

Example 21 : Match a word boundary

pattern = r"\bword\b"
text = "This is a word in a sentence."
print(re.search(pattern, text))

Example 22 : Match a non-word boundary

pattern = r"\Bword\B"
text = "This is a word in a sentence."
print(re.search(pattern, text))

Example 23 : Match a specific character set

pattern = r"[aeiou]"
text = "This is a test."
    print(re.findall(pattern, text))

Example 24 : Match a specific range of characters

pattern = r"[a-z]"
text = "This is a test."
    print(re.findall(pattern, text))

Example 25 : Match a specific character class

pattern = r"\d{3}-\d{2}-\d{4}"
text = "My SSN is 123-45-6789"
    print(re.search(pattern, text))

Example 26 : Match a specific character set with negation

pattern = r"[^aeiou]"
text = "This is a test."
    print(re.findall(pattern, text))

Example 27 : Match a specific character set with quantifiers

pattern = r"[a-z]{2,4}"
text = "This is a test."
    print(re.findall(pattern, text))

Example 28 : Match a specific character set with repetition

pattern = r"[a-z]{2,}"
text = "This is a test."

    print(re.findall(pattern, text))

Example 29 : Match a specific character set with optional characters

pattern = r"[a-z]{2,4}?"
text = "This is a test."
    print(re.findall(pattern, text))

Example 30 : Match a specific character set with greedy matching

pattern = r"[a-z]{2,4}+"
text = "This is a test."

    print(re.findall(pattern, text))