Combined Character Classes

UpComing SoftWare Training Demos

πŸš€ *Gen AI EngineerΒ Telugu (Production Focused)*
πŸ—“οΈ *Date:* 30th Sept 2026, 07:00AM IST
πŸ“ *Register Now!:*
https://www.vlrt.in/gr
πŸ‘₯ *Join WA Community:*
https://www.vlrt.in/gw
πŸ’‘*Course Content:*
https://www.vlrt.in/ai
▢️ *Demo Videos:*
https://www.vlrt.in/gv

πŸš€ *Service now Admin/ Development (ITSM)FREE Demo in Telugu*
πŸ—“οΈ *Date:* 30th Sept 2026, 08:00AM IST
πŸ“ *Register Now!:*
https://www.vlrt.in/7r
πŸ‘₯ *Join WA Community:*
https://www.vlrt.in/7w
πŸ’‘*Course Content:*
https://www.vlrt.in/7c
▢️ *Demo Videos:*
https://www.vlrt.in/7v

πŸš€ *Vulnerability Management Training Demo*
πŸ—“οΈ *Date:* 30th Sept 2026, 09:00 AM IST
πŸ“*Register Now!:*
https://www.vlrt.in/vr
πŸ‘₯ *Join Community:*
https://www.vlrt.in/cw
πŸ’‘ *Course Content:*
https://www.vlrt.in/vc
▢️ *Demo Videos:*
https://www.vlrt.in/Vm

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

  • re.sub()

    Python re.sub() Method Explained The re.sub() method is used for searching and replacing text patterns in strings. It’s one of the most powerful regex methods for text processing. Syntax python re.sub(pattern, repl, string, count=0, flags=0) Example 1: Basic Text Replacement python import re text = “The color of the sky is blue. My favorite color is blue too.” #…

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

  • TheΒ Fractions module

    The Fractions module in Python is a built-in module that provides support for rational number arithmetic. It allows you to work with fractions (like 1/2, 3/4, etc.) exactly, without the precision issues that can occur with floating-point numbers. What Problems Does It Solve? Problem with Floating-Point Numbers: python # Floating-point precision issue print(0.1 + 0.2) # Output:…

  • reΒ module

    The re module is Python’s built-in module for regular expressions (regex). It provides functions and methods to work with strings using pattern matching, allowing you to search, extract, replace, and split text based on complex patterns. Key Functions in the re Module 1. Searching and Matching python import re text = “The quick brown fox jumps over the lazy dog” # re.search()…

  • Data hiding

    Data hiding in Python OOP is the concept of restricting access to the internal data of an object from outside the class. πŸ” It’s a way to prevent direct modification of data and protect the object’s integrity. This is typically achieved by using a naming convention that makes attributes “private” or “protected.” πŸ”’ How Data…

  • Python Installation Guide: Easy Steps for Windows, macOS, and Linux

    Installing Python is a straightforward process, and it can be done on various operating systems like Windows, macOS, and Linux. Below are step-by-step instructions for installing Python on each platform. 1. Installing Python on Windows Step 1: Download Python Step 2: Run the Installer Step 3: Verify Installation If Python is installed correctly, it will…

Leave a Reply

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