Combined Character Classes

Combined Character Classes Explained with Examples

1. [a-zA-Z0-9_] – Word characters (same as \w)

Description: Matches any letter (lowercase or uppercase), any digit, or underscore

Example 1: Extract all word characters from text

python

import re
text = "User_name123! Email: test@example.com"
result = re.findall(r'[a-zA-Z0-9_]', text)
print(result)  
# ['U', 's', 'e', 'r', '_', 'n', 'a', 'm', 'e', '1', '2', '3', 'E', 'm', 'a', 'i', 'l', 't', 'e', 's', 't', 'e', 'x', 'a', 'm', 'p', 'l', 'e', 'c', 'o', 'm']
# All letters, digits, and underscores

Example 2: Validate username format

python

usernames = ["john_doe123", "user-name", "Admin123", "test@user"]
valid_usernames = [user for user in usernames if re.fullmatch(r'[a-zA-Z0-9_]+', user)]
print(valid_usernames)  # ['john_doe123', 'Admin123']
# Only usernames with letters, digits, and underscores

Example 3: Extract hashtags (alphanumeric with underscore)

python

text = "#Python #coding_101 #machine-learning #AI_2024"
result = re.findall(r'#[a-zA-Z0-9_]+', text)
print(result)  # ['#Python', '#coding_101', '#AI_2024']
# Hashtags containing only word characters

Example 4: Split on non-word characters

python

text = "Hello, World! How_are_you? Test123."
words = re.findall(r'[a-zA-Z0-9_]+', text)
print(words)  # ['Hello', 'World', 'How_are_you', 'Test123']
# Extract all word sequences

2. [0-9a-fA-F] – Hexadecimal digits

Description: Matches any hexadecimal digit (0-9, a-f, A-F)

Example 1: Extract hexadecimal color codes

python

text = "Colors: #FF0000, #00ff00, #0000FF, #abc123, #XYZ999"
result = re.findall(r'#[0-9a-fA-F]{6}', text)
print(result)  # ['#FF0000', '#00ff00', '#0000FF', '#abc123']
# Valid 6-digit hex color codes

Example 2: Find hexadecimal numbers in text

python

text = "Values: 0x1A3F, 0xFF, 0xabc, 123, 0xGHI"
result = re.findall(r'0x[0-9a-fA-F]+', text)
print(result)  # ['0x1A3F', '0xFF', '0xabc']
# Hexadecimal numbers starting with 0x

Example 3: Validate MAC address format

python

mac_address = "00:1A:2B:3C:4D:5E"
is_valid = bool(re.fullmatch(r'([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}', mac_address))
print(is_valid)  # True
# Valid MAC address format

Example 4: Extract CSS color values

python

css = "color: #f00; background: #ffffff; border: #abc123; invalid: #xyz"
result = re.findall(r'#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})', css)
print(result)  # ['f00', 'ffffff', 'abc123']
# 3-digit or 6-digit hex colors

3. [aeiou] – Any vowel

Description: Matches any vowel character (lowercase only in this case)

Example 1: Count vowels in text

python

text = "Hello World, how are you today?"
vowels = re.findall(r'[aeiou]', text, re.IGNORECASE)
print(vowels)  # ['e', 'o', 'o', 'o', 'a', 'e', 'o', 'u', 'o', 'a']
# All vowels (case-insensitive)
print("Total vowels:", len(vowels))  # Total vowels: 10

Example 2: Find words with multiple vowels

python

text = "Beautiful universe, rhythm sync, why try?"
result = re.findall(r'\b\w*[aeiou]\w*[aeiou]\w*\b', text, re.IGNORECASE)
print(result)  # ['Beautiful', 'universe', 'rhythm']
# Words containing at least 2 vowels

Example 3: Remove all vowels from text

python

text = "Programming is fun with Python!"
no_vowels = re.sub(r'[aeiouAEIOU]', '', text)
print(no_vowels)  # "Prgrmmng s fn wth Pythn!"
# Text with all vowels removed

Example 4: Check if word starts with vowel

python

words = ["apple", "banana", "orange", "umbrella", "hello"]
vowel_start = [word for word in words if re.match(r'^[aeiouAEIOU]', word)]
print(vowel_start)  # ['apple', 'orange', 'umbrella']
# Words starting with vowels

4. [^aeiou] – Any non-vowel

Description: Matches any character that is NOT a vowel

Example 1: Extract consonants and other characters

python

text = "Hello World! 123"
non_vowels = re.findall(r'[^aeiouAEIOU]', text)
print(non_vowels)  # ['H', 'l', 'l', ' ', 'W', 'r', 'l', 'd', '!', ' ', '1', '2', '3']
# All characters except vowels

Example 2: Find words without vowels

python

text = "sky rhythm fly why crypt my"
result = re.findall(r'\b[^aeiouAEIOU ]+\b', text)
print(result)  # ['sky', 'fly', 'why', 'crypt', 'my']
# Words that might not contain vowels (English has few of these)

Example 3: Remove vowels keeping punctuation

python

text = "Hello, World! How are you?"
result = re.sub(r'[aeiouAEIOU]', '', text)
print(result)  # "Hll, Wrld! Hw r y?"
# Text with vowels removed but punctuation preserved

Example 4: Count non-vowel characters

python

text = "Python Programming"
non_vowels = re.findall(r'[^aeiouAEIOU]', text)
print("Non-vowel characters:", len(non_vowels))  # Non-vowel characters: 12
# Count of consonants, spaces, and other non-vowels

5. [.!?] – Punctuation marks

Description: Matches period, exclamation mark, or question mark

Example 1: Find all sentence endings

python

text = "Hello! How are you? I'm fine. Great!"
punctuation = re.findall(r'[.!?]', text)
print(punctuation)  # ['!', '?', '.', '!']
# All sentence-ending punctuation

Example 2: Split text into sentences

python

text = "First sentence. Second sentence! Third sentence? Fourth one."
sentences = re.split(r'[.!?]', text)
sentences = [s.strip() for s in sentences if s.strip()]
print(sentences)  # ['First sentence', 'Second sentence', 'Third sentence', 'Fourth one']
# Text split into sentences

Example 3: Count different types of sentences

python

text = "Wow! Really? Amazing. Incredible! Is that true? Wonderful."
questions = len(re.findall(r'\?', text))
exclamations = len(re.findall(r'!', text))
statements = len(re.findall(r'\.', text))
print(f"Questions: {questions}, Exclamations: {exclamations}, Statements: {statements}")
# Questions: 2, Exclamations: 2, Statements: 2

Example 4: Validate sentence ending

python

sentences = ["Hello world.", "What's up!", "No ending", "Why not?"]
valid_ends = [s for s in sentences if re.search(r'[.!?]$', s)]
print(valid_ends)  # ["Hello world.", "What's up!", "Why not?"]
# Sentences that properly end with punctuation

6. [+-] – Plus or minus sign

Description: Matches either plus (+) or minus (-) sign

Example 1: Find mathematical operators

python

text = "5 + 3 - 2 * 4 / 1 + 6 - 0"
operators = re.findall(r'[+-]', text)
print(operators)  # ['+', '-', '+', '-']
# Only plus and minus operators (not * or /)

Example 2: Extract signed numbers

python

text = "Temperatures: +25°C, -5°C, 0°C, +30°C, -10°C"
signed_numbers = re.findall(r'[+-]\d+', text)
print(signed_numbers)  # ['+25', '-5', '+30', '-10']
# Numbers with explicit signs

Example 3: Validate mathematical expressions

python

expressions = ["5+3", "7-2", "4*1", "8/2", "9+0"]
valid_ops = [expr for expr in expressions if re.search(r'[+-]', expr)]
print(valid_ops)  # ['5+3', '7-2', '9+0']
# Expressions using only + or - operators

Example 4: Parse CSV with optional signs

python

data = "+100,-50,75,-25,+300,150"
values = re.findall(r'([+-]?\d+)', data)
print(values)  # ['+100', '-50', '75', '-25', '+300', '150']
# Numbers with optional signs
numeric_values = [int(val) for val in values]
print(numeric_values)  # [100, -50, 75, -25, 300, 150]

Bonus: Combined Examples

Example: Complex pattern using multiple character classes

python

text = "User: john_doe123, Score: +100, Email: john@example.com, Status: active"

# Extract different types of data
username = re.findall(r'User: ([a-zA-Z0-9_]+)', text)
score = re.findall(r'Score: ([+-]?\d+)', text)
email = re.findall(r'Email: ([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})', text)
status = re.findall(r'Status: ([a-zA-Z]+)', text)

print("Username:", username)  # ['john_doe123']
print("Score:", score)        # ['+100']
print("Email:", email)        # ['john@example.com']
print("Status:", status)      # ['active']

Similar Posts

  • Quantifiers (Repetition)

    Quantifiers (Repetition) in Python Regular Expressions – Detailed Explanation Basic Quantifiers 1. * – 0 or more occurrences (Greedy) Description: Matches the preceding element zero or more times Example 1: Match zero or more digits python import re text = “123 4567 89″ result = re.findall(r’\d*’, text) print(result) # [‘123’, ”, ‘4567’, ”, ’89’, ”] # Matches…

  • File Handling in Python

    File Handling in Python File handling is a crucial aspect of programming that allows you to read from and write to files. Python provides built-in functions and methods to work with files efficiently. Basic File Operations 1. Opening a File Use the open() function to open a file. It returns a file object. python # Syntax: open(filename,…

  • Lambda Functions in Python

    Lambda Functions in Python Lambda functions are small, anonymous functions defined using the lambda keyword. They can take any number of arguments but can only have one expression. Basic Syntax python lambda arguments: expression Simple Examples 1. Basic Lambda Function python # Regular function def add(x, y): return x + y # Equivalent lambda function add_lambda =…

  • Indexing and Slicing for Writing (Modifying) Lists in Python

    Indexing and Slicing for Writing (Modifying) Lists in Python Indexing and slicing aren’t just for reading lists – they’re powerful tools for modifying lists as well. Let’s explore how to use them to change list contents with detailed examples. 1. Modifying Single Elements (Indexing for Writing) You can directly assign new values to specific indices. Example 1:…

  • AttributeError: ‘NoneType’ Error in Python re

    AttributeError: ‘NoneType’ Error in Python re This error occurs when you try to call match object methods on None instead of an actual match object. It’s one of the most common errors when working with Python’s regex module. Why This Happens: The re.search(), re.match(), and re.fullmatch() functions return: When you try to call methods like .group(), .start(), or .span() on None, you get this error. Example That Causes…

  • Anchors (Position Matchers)

    Anchors (Position Matchers) in Python Regular Expressions – Detailed Explanation Basic Anchors 1. ^ – Start of String/Line Anchor Description: Matches the start of a string, or start of any line when re.MULTILINE flag is used Example 1: Match at start of string python import re text = “Python is great\nPython is powerful” result = re.findall(r’^Python’, text) print(result) #…

Leave a Reply

Your email address will not be published. Required fields are marked *