Basic 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

Basic Character Classes

PatternDescriptionExample Matches
[abc]Matches any single character in the bracketsa, b, or c
[^abc]Matches any single character NOT in the bracketsd, 1, ! (not a, b, or c)
[a-z]Matches any character in the range a to za, b, c, …, z
[A-Z]Matches any character in the range A to ZA, B, C, …, Z
[0-9]Matches any digit0, 1, 2, …, 9
[a-zA-Z]Matches any letter (lowercase or uppercase)a, B, z, Y
[a-z0-9]Matches any lowercase letter or digita, 5, z, 9
[A-Z0-9]Matches any uppercase letter or digitA, 5, Z, 9

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

  • Dictionaries

    Python Dictionaries: Explanation with Examples A dictionary in Python is an unordered collection of items that stores data in key-value pairs. Dictionaries are: Creating a Dictionary python # Empty dictionary my_dict = {} # Dictionary with initial values student = { “name”: “John Doe”, “age”: 21, “courses”: [“Math”, “Physics”, “Chemistry”], “GPA”: 3.7 } Accessing Dictionary Elements…

  • Formatted printing

    C-Style String Formatting in Python Python supports C-style string formatting using the % operator, which provides similar functionality to C’s printf() function. This method is sometimes called “old-style” string formatting but remains useful in many scenarios. Basic Syntax python “format string” % (values) Control Characters (Format Specifiers) Format Specifier Description Example Output %s String “%s” % “hello” hello %d…

  • List of Basic Regular Expression Patterns in Python

    Complete List of Basic Regular Expression Patterns in Python Character Classes Pattern Description Example [abc] Matches any one of the characters a, b, or c [aeiou] matches any vowel [^abc] Matches any character except a, b, or c [^0-9] matches non-digits [a-z] Matches any character in range a to z [a-z] matches lowercase letters [A-Z] Matches any character in range…

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

  • Iterators in Python

    Iterators in Python An iterator in Python is an object that is used to iterate over iterable objects like lists, tuples, dictionaries, and sets. An iterator can be thought of as a pointer to a container’s elements. To create an iterator, you use the iter() function. To get the next element from the iterator, you…

Leave a Reply

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