Basic Character Classes

Basic Character Classes

PatternDescriptionExample Matches
[abc]Matches any single character in the bracketsab, or c
[^abc]Matches any single character NOT in the bracketsd1! (not a, b, or c)
[a-z]Matches any character in the range a to zabc, …, z
[A-Z]Matches any character in the range A to ZABC, …, Z
[0-9]Matches any digit012, …, 9
[a-zA-Z]Matches any letter (lowercase or uppercase)aBzY
[a-z0-9]Matches any lowercase letter or digita5z9
[A-Z0-9]Matches any uppercase letter or digitA5Z9

1. [abc] – Matches any single character in the brackets

Description: Matches exactly one character that is either ‘a’, ‘b’, or ‘c’

Example 1: Find vowels in a word

python

import re
text = "hello world"
result = re.findall(r'[aeiou]', text)
print(result)  # ['e', 'o', 'o']
# Matches all vowels in the text

Example 2: Find specific punctuation

python

text = "Hello! How are you? I'm fine."
result = re.findall(r'[!?.]', text)
print(result)  # ['!', '?', '.']
# Matches specific punctuation marks

Example 3: Find digits 1, 2, or 3

python

text = "Phone: 123-456-7890"
result = re.findall(r'[123]', text)
print(result)  # ['1', '2', '3']
# Matches only digits 1, 2, and 3

Example 4: Find specific letters (case-sensitive)

python

text = "Apple Banana cherry"
result = re.findall(r'[AB]', text)
print(result)  # ['A', 'B']
# Matches only uppercase A and B

2. [^abc] – Matches any single character NOT in the brackets

Description: Matches any character except ‘a’, ‘b’, or ‘c’

Example 1: Find non-vowels

python

text = "hello world"
result = re.findall(r'[^aeiou]', text)
print(result)  # ['h', 'l', 'l', ' ', 'w', 'r', 'l', 'd']
# All characters except vowels

Example 2: Find non-digits

python

text = "Room 101, Floor 2"
result = re.findall(r'[^0-9]', text)
print(result)  # ['R', 'o', 'o', 'm', ' ', ',', ' ', 'F', 'l', 'o', 'o', 'r', ' ']
# All characters except digits

Example 3: Exclude specific punctuation

python

text = "Hello! How are you? I'm fine, thanks."
result = re.findall(r'[^!?,]', text)
print(''.join(result))  # "Hello How are you I'm fine thanks."
# All characters except !, ?, and ,

Example 4: Exclude specific letters

python

text = "abcxyz123"
result = re.findall(r'[^abc]', text)
print(result)  # ['x', 'y', 'z', '1', '2', '3']
# All characters except a, b, and c

3. [a-z] – Matches any character in the range a to z

Description: Matches any lowercase letter from a to z

Example 1: Find all lowercase letters

python

text = "Hello World 123"
result = re.findall(r'[a-z]', text)
print(result)  # ['e', 'l', 'l', 'o', 'o', 'r', 'l', 'd']
# All lowercase letters

Example 2: Find lowercase words

python

text = "Python is AWESOME for coding"
result = re.findall(r'[a-z]+', text)
print(result)  # ['ython', 'is', 'for', 'coding']
# Lowercase words (note: 'Python' becomes 'ython' because P is uppercase)

Example 3: Validate username (lowercase only)

python

username = "john_doe123"
is_valid = bool(re.fullmatch(r'[a-z0-9_]+', username))
print(is_valid)  # True
# Checks if username contains only lowercase letters, digits, and underscores

Example 4: Extract file extensions (lowercase)

python

files = "image.JPG document.txt script.PY readme.md"
result = re.findall(r'\.([a-z]+)', files)
print(result)  # ['txt', 'md']
# Only lowercase file extensions

4. [A-Z] – Matches any character in the range A to Z

Description: Matches any uppercase letter from A to Z

Example 1: Find all uppercase letters

python

text = "Hello World from PYTHON"
result = re.findall(r'[A-Z]', text)
print(result)  # ['H', 'W', 'P', 'Y', 'T', 'H', 'O', 'N']
# All uppercase letters

Example 2: Find acronyms and abbreviations

python

text = "The USA and UK are countries. NASA is a space agency."
result = re.findall(r'[A-Z]{2,}', text)
print(result)  # ['USA', 'UK', 'NASA']
# Words with 2 or more uppercase letters

Example 3: Find sentence starters

python

text = "hello. World! how are you? I am fine."
result = re.findall(r'[A-Z][a-z]*', text)
print(result)  # ['World', 'I']
# Words that start with uppercase letter (likely sentence starters)

Example 4: Validate product codes

python

codes = ["AB123", "CD456", "ef789", "GH-001"]
valid_codes = [code for code in codes if re.fullmatch(r'[A-Z]{2}\d{3}', code)]
print(valid_codes)  # ['AB123', 'CD456']
# Codes with exactly 2 uppercase letters followed by 3 digits

5. [0-9] – Matches any digit

Description: Matches any digit from 0 to 9

Example 1: Extract all digits

python

text = "Phone: 123-456-7890, Age: 25"
result = re.findall(r'[0-9]', text)
print(result)  # ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '2', '5']
# All individual digits

Example 2: Find numbers with specific digits

python

text = "Scores: 95, 87, 73, 64, 59, 100"
result = re.findall(r'[5-9][0-9]', text)
print(result)  # ['95', '87', '73', '64', '59']
# Numbers between 50-99

Example 3: Validate PIN code

python

pin = "1234"
is_valid = bool(re.fullmatch(r'[0-9]{4}', pin))
print(is_valid)  # True
# 4-digit PIN code validation

Example 4: Extract years from text

python

text = "Events in 1999, 2005, 2010, and 2020"
result = re.findall(r'[12][0-9]{3}', text)
print(result)  # ['1999', '2005', '2010', '2020']
# Years between 1000-2999

6. [a-zA-Z] – Matches any letter (lowercase or uppercase)

Description: Matches any letter regardless of case

Example 1: Extract all letters

python

text = "Hello World 123! @#$"
result = re.findall(r'[a-zA-Z]', text)
print(result)  # ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
# All letters, ignoring case

Example 2: Find words (letters only)

python

text = "Python3 is better than Java8!"
result = re.findall(r'[a-zA-Z]+', text)
print(result)  # ['Python', 'is', 'better', 'than', 'Java']
# Words consisting only of letters

Example 3: Validate name field

python

name = "John Doe"
is_valid = bool(re.fullmatch(r'[a-zA-Z ]+', name))
print(is_valid)  # True
# Name containing only letters and spaces

Example 4: Extract capitalized words

python

text = "The Quick Brown Fox jumps Over The Lazy Dog"
result = re.findall(r'[A-Z][a-z]*', text)
print(result)  # ['The', 'Quick', 'Brown', 'Fox', 'Over', 'The', 'Lazy', 'Dog']
# Words that start with uppercase letter

7. [a-z0-9] – Matches any lowercase letter or digit

Description: Matches lowercase letters or digits

Example 1: Extract alphanumeric characters (lowercase)

python

text = "User123: john_doe, Email: test@example.com"
result = re.findall(r'[a-z0-9]', text)
print(result)  # ['s', 'e', 'r', '1', '2', '3', 'j', 'o', 'h', 'n', 'd', 'o', 'e', 'e', 's', 't', 'e', 'x', 'a', 'm', 'p', 'l', 'e', 'c', 'o', 'm']
# Lowercase letters and digits only

Example 2: Validate username (lowercase alphanumeric)

python

username = "john123"
is_valid = bool(re.fullmatch(r'[a-z0-9]+', username))
print(is_valid)  # True
# Username with only lowercase letters and digits

Example 3: Extract hashtags (lowercase)

python

text = "#Python #coding #PROGRAMMING #java123"
result = re.findall(r'#[a-z0-9]+', text)
print(result)  # ['#coding', '#java123']
# Lowercase hashtags with numbers

Example 4: Find version numbers

python

text = "Versions: v1.2.3, V2.5, v10.0.1"
result = re.findall(r'v([a-z0-9.]+)', text, re.IGNORECASE)
print(result)  # ['1.2.3', '2.5', '10.0.1']
# Version numbers after 'v' (case-insensitive)

8. [A-Z0-9] – Matches any uppercase letter or digit

Description: Matches uppercase letters or digits

Example 1: Extract uppercase alphanumeric characters

python

text = "Product Code: AB123, Serial: XYZ456-789"
result = re.findall(r'[A-Z0-9]', text)
print(result)  # ['P', 'C', 'A', 'B', '1', '2', '3', 'S', 'X', 'Y', 'Z', '4', '5', '6', '7', '8', '9']
# Uppercase letters and digits only

Example 2: Validate license plate format

python

plate = "AB123CD"
is_valid = bool(re.fullmatch(r'[A-Z0-9]+', plate))
print(is_valid)  # True
# License plate with uppercase letters and digits

Example 3: Extract stock symbols

python

text = "Stocks: AAPL, GOOGL, MSFT, TSLA123"
result = re.findall(r'[A-Z0-9]{2,}', text)
print(result)  # ['AAPL', 'GOOGL', 'MSFT', 'TSLA123']
# Uppercase stock symbols with possible numbers

Example 4: Find hexadecimal numbers (uppercase)

python

text = "Colors: #FF0000, #00FF00, #0000FF, #abcdef"
result = re.findall(r'#[A-F0-9]{6}', text)
print(result)  # ['#FF0000', '#00FF00', '#0000FF']
# Uppercase hexadecimal color codes

Similar Posts

  • Operator Overloading

    Operator Overloading in Python with Simple Examples Operator overloading allows you to define how Python operators (like +, -, *, etc.) work with your custom classes. This makes your objects behave more like built-in types. 1. What is Operator Overloading? 2. Basic Syntax python class ClassName: def __special_method__(self, other): # Define custom behavior 3. Common Operator Overloading Methods Operator…

  • Formatting Date and Time in Python

    Formatting Date and Time in Python Python provides powerful formatting options for dates and times using the strftime() method and parsing using strptime() method. 1. Basic Formatting with strftime() Date Formatting python from datetime import date, datetime # Current date today = date.today() print(“Date Formatting Examples:”) print(f”Default: {today}”) print(f”YYYY-MM-DD: {today.strftime(‘%Y-%m-%d’)}”) print(f”MM/DD/YYYY: {today.strftime(‘%m/%d/%Y’)}”) print(f”DD-MM-YYYY: {today.strftime(‘%d-%m-%Y’)}”) print(f”Full month: {today.strftime(‘%B %d, %Y’)}”) print(f”Abbr…

  • Case Conversion Methods in Python

    Case Conversion Methods in Python (Syntax + Examples) Python provides several built-in string methods to convert text between different cases (uppercase, lowercase, title case, etc.). Below are the key methods with syntax and examples: 1. upper() – Convert to Uppercase Purpose: Converts all characters in a string to uppercase.Syntax: python string.upper() Examples: python text = “Hello, World!”…

  • Curly Braces {} ,Pipe (|) Metacharacters

    Curly Braces {} in Python Regex Curly braces {} are used to specify exact quantity of the preceding character or group. They define how many times something should appear. Basic Syntax: Example 1: Exact Number of Digits python import re text = “Zip codes: 12345, 9876, 123, 123456, 90210″ # Match exactly 5 digits pattern = r”\d{5}” # Exactly…

  • Programs

    Weekly Wages Removing Duplicates even ,odd Palindrome  Rotate list Shuffle a List Python random Module Explained with Examples The random module in Python provides functions for generating pseudo-random numbers and performing random operations. Here’s a detailed explanation with three examples for each important method: Basic Random Number Generation 1. random.random() Returns a random float between 0.0 and 1.0 python import…

Leave a Reply

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