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

  • Thonny: A User-Friendly Python IDE for Beginners in 2025

    Thonny is a free and open-source Integrated Development Environment (IDE) specifically designed for beginners learning Python. It provides a simple and user-friendly interface, making it an excellent choice for those new to programming. Key Features: Why Thonny is good for beginners: How to install Thonny: If you’re new to Python and looking for a user-friendly…

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

  • start(), end(), and span()

    Python re start(), end(), and span() Methods Explained These methods are used with match objects to get the positional information of where a pattern was found in the original string. They work on the result of re.search(), re.match(), or re.finditer(). Methods Overview: Example 1: Basic Position Tracking python import re text = “The quick brown fox jumps over the lazy…

  • While loop

    A while loop in Python is used to repeatedly execute a block of code as long as a given condition remains True. It is particularly useful when you don’t know beforehand how many times the loop needs to run. Before every iteration, Python evaluates the condition. If it evaluates to True, the code inside the…

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

  • re.I, re.S, re.X

    Python re Flags: re.I, re.S, re.X Explained Flags modify how regular expressions work. They’re used as optional parameters in re functions like re.search(), re.findall(), etc. 1. re.I or re.IGNORECASE Purpose: Makes the pattern matching case-insensitive Without re.I (Case-sensitive): python import re text = “Hello WORLD hello World” # Case-sensitive search matches = re.findall(r’hello’, text) print(“Case-sensitive:”, matches) # Output: [‘hello’] # Only finds lowercase…

Leave a Reply

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