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

  • Polymorphism

    Polymorphism is a core concept in OOP that means “many forms” 🐍. In Python, it allows objects of different classes to be treated as objects of a common superclass. This means you can use a single function or method to work with different data types, as long as they implement a specific action. 🌀 Polymorphism…

  • Functions Returning Functions

    Understanding Functions Returning Functions In Python, functions can return other functions, which is a powerful feature of functional programming. Basic Example python def outer(): def inner(): print(“Welcome!”) return inner # Return the inner function (without calling it) # Calling outer() returns the inner function f = outer() # f now refers to the inner function…

  • Else Block in Exception Handling in Python

    Else Block in Exception Handling in Python The else block in Python exception handling executes only if the try block completes successfully without any exceptions. It’s placed after all except blocks and before the finally block. Basic Syntax: python try: # Code that might raise an exception except SomeException: # Handle the exception else: # Code that runs only if no exception…

  •  List operators,List Traversals

    In Python, lists are ordered, mutable collections that support various operations. Here are the key list operators along with four basic examples: List Operators in Python 4 Basic Examples 1. Concatenation (+) Combines two lists into one. python list1 = [1, 2, 3] list2 = [4, 5, 6] combined = list1 + list2 print(combined) # Output: [1, 2, 3,…

  • Keyword-Only Arguments in Python and mixed

    Keyword-Only Arguments in Python Keyword-only arguments are function parameters that must be passed using their keyword names. They cannot be passed as positional arguments. Syntax Use the * symbol in the function definition to indicate that all parameters after it are keyword-only: python def function_name(param1, param2, *, keyword_only1, keyword_only2): # function body Simple Examples Example 1: Basic Keyword-Only Arguments…

  • sqlite3 create table

    The sqlite3 module is the standard library for working with the SQLite database in Python. It provides an interface compliant with the DB-API 2.0 specification, allowing you to easily connect to, create, and interact with SQLite databases using SQL commands directly from your Python code. It is particularly popular because SQLite is a serverless database…

Leave a Reply

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