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

  • Method overriding

    Method overriding is a key feature of object-oriented programming (OOP) and inheritance. It allows a subclass (child class) to provide its own specific implementation of a method that is already defined in its superclass (parent class). When a method is called on an object of the child class, the child’s version of the method is…

  • String Validation Methods

    Complete List of Python String Validation Methods Python provides several built-in string methods to check if a string meets certain criteria. These methods return True or False and are useful for input validation, data cleaning, and text processing. 1. Case Checking Methods Method Description Example isupper() Checks if all characters are uppercase “HELLO”.isupper() → True islower() Checks if all…

  • install Python, idle, Install pycharm

    Step 1: Download Python Step 2: Install Python Windows macOS Linux (Debian/Ubuntu) Open Terminal and run: bash Copy Download sudo apt update && sudo apt install python3 python3-pip For Fedora/CentOS: bash Copy Download sudo dnf install python3 python3-pip Step 3: Verify Installation Open Command Prompt (Windows) or Terminal (macOS/Linux) and run: bash Copy Download python3 –version # Should show…

  • How to create Class

    🟥 Rectangle Properties Properties are the nouns that describe a rectangle. They are the characteristics that define a specific rectangle’s dimensions and position. Examples: 📐 Rectangle Methods Methods are the verbs that describe what a rectangle can do or what can be done to it. They are the actions that allow you to calculate information…

  • Method Overloading

    Python does not support traditional method overloading in the way languages like C++ or Java do. If you define multiple methods with the same name, the last definition will simply overwrite all previous ones. However, you can achieve the same result—making a single method behave differently based on the number or type of arguments—using Python’s…

Leave a Reply

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