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

  • Generalization vs. Specialization

    Object-Oriented Programming: Generalization vs. Specialization Introduction Inheritance in OOP serves two primary purposes: Let’s explore these concepts with clear examples. 1. Specialization (Extending Functionality) Specialization involves creating a new class that inherits all features from a parent class and then adds new, specific features. The core idea is reusability—you build upon what already exists. Key Principle: Child Class =…

  • Create lists

    In Python, there are multiple ways to create lists, depending on the use case. Below are the most common methods: 1. Direct Initialization (Using Square Brackets []) The simplest way to create a list is by enclosing elements in square brackets []. Example: python empty_list = [] numbers = [1, 2, 3, 4] mixed_list = [1, “hello”, 3.14,…

  • 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 =…

  • Challenge Summary: Inheritance – Polygon and Triangle Classes

    Challenge Summary: Inheritance – Polygon and Triangle Classes Objective: Create two classes where Triangle inherits from Polygon and calculates area using Heron’s formula. 1. Polygon Class (Base Class) Properties: Methods: __init__(self, num_sides, *sides) python class Polygon: def __init__(self, num_sides, *sides): self.number_of_sides = num_sides self.sides = list(sides) 2. Triangle Class (Derived Class) Inheritance: Methods: __init__(self, *sides) area(self) python import math…

  • Class Variables Andmethds

    Class Variables Class variables are variables that are shared by all instances of a class. They are defined directly within the class but outside of any method. Unlike instance variables, which are unique to each object, a single copy of a class variable is shared among all objects of that class. They are useful for…

Leave a Reply

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