List of Basic Regular Expression Patterns in Python

Complete List of Basic Regular Expression Patterns in Python

Character Classes

PatternDescriptionExample
[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 A to Z[A-Z] matches uppercase letters
[0-9]Matches any digit[0-9] matches digits 0-9
[a-zA-Z]Matches any letter[a-zA-Z] matches all letters
[a-zA-Z0-9_]Equivalent to \wMatches word characters

Predefined Character Classes

PatternDescriptionEquivalent
.Matches any character except newline
\dMatches any digit[0-9]
\DMatches any non-digit[^0-9]
\wMatches any word character[a-zA-Z0-9_]
\WMatches any non-word character[^a-zA-Z0-9_]
\sMatches any whitespace character[ \t\n\r\f\v]
\SMatches any non-whitespace character[^ \t\n\r\f\v]

Anchors (Position Matchers)

PatternDescriptionExample
^Matches start of string (or line with re.MULTILINE)^Hello matches “Hello” at start
$Matches end of string (or line with re.MULTILINE)world$ matches “world” at end
\bMatches word boundary\bword\b matches “word” as whole word
\BMatches non-word boundary\Bword\B matches “word” inside other words

Quantifiers (Repetition)

PatternDescriptionExample
*0 or more occurrencesa* matches “”, “a”, “aa”, “aaa”, …
+1 or more occurrencesa+ matches “a”, “aa”, “aaa”, …
?0 or 1 occurrencea? matches “” or “a”
{n}Exactly n occurrencesa{3} matches “aaa”
{n,}n or more occurrencesa{2,} matches “aa”, “aaa”, “aaaa”, …
{n,m}Between n and m occurrencesa{2,4} matches “aa”, “aaa”, “aaaa”

Alternation and Grouping

PatternDescriptionExample
``OR operator (alternation)`catdog` matches “cat” or “dog”
()Grouping and capturing(abc)+ matches “abc”, “abcabc”, etc.
(?:)Non-capturing group(?:abc)+ groups without capturing

Escape Sequences

PatternDescriptionExample
\\Backslash\\ matches “”
\.Literal dot\. matches “.”
\*Literal asterisk\* matches “*”
\+Literal plus\+ matches “+”
\?Literal question mark\? matches “?”
\(Literal opening parenthesis\( matches “(“
\)Literal closing parenthesis\) matches “)”
\[Literal opening bracket\[ matches “[“
\]Literal closing bracket\] matches “]”
\{Literal opening brace\{ matches “{“
\}Literal closing brace\} matches “}”
\^Literal caret\^ matches “^”
\$Literal dollar\$ matches “$”
|Literal pipe| matches “

Special Sequences

PatternDescriptionExample
\AMatches only at start of string\AStart
\ZMatches only at end of stringend\Z
\nNewline character
\tTab character
\rCarriage return
\fForm feed
\vVertical tab

Practical Examples with Each Pattern Type

python

import re

# Character Classes
text = "abc123XYZ"
print(re.findall(r'[a-z]', text))        # ['a', 'b', 'c']
print(re.findall(r'[^0-9]', text))       # ['a', 'b', 'c', 'X', 'Y', 'Z']

# Predefined Classes
text = "Hello 123 World!"
print(re.findall(r'\d', text))           # ['1', '2', '3']
print(re.findall(r'\w', text))           # ['H', 'e', 'l', 'l', 'o', '1', '2', '3', 'W', 'o', 'r', 'l', 'd']

# Anchors
text = "Hello world\nHello python"
print(re.findall(r'^Hello', text))       # ['Hello'] (start of string)
print(re.findall(r'^Hello', text, re.MULTILINE))  # ['Hello', 'Hello'] (start of each line)

# Quantifiers
text = "a aa aaa aaaa"
print(re.findall(r'a{2,3}', text))       # ['aa', 'aaa', 'aaa']

# Alternation
text = "I have a cat and a dog"
print(re.findall(r'cat|dog', text))      # ['cat', 'dog']

# Groups
text = "abcabc abc"
print(re.findall(r'(abc)+', text))       # ['abc'] (captures last group)
print(re.findall(r'(?:abc)+', text))     # ['abcabc', 'abc'] (non-capturing)

# Escape Sequences
text = "Price: $10.99 + tax"
print(re.findall(r'\$\d+\.\d+', text))   # ['$10.99']

Common Pattern Combinations

python

# Email pattern
email_pattern = r'[\w\.-]+@[\w\.-]+\.\w+'

# Phone number pattern (basic)
phone_pattern = r'\d{3}[-.]\d{3}[-.]\d{4}'

# URL pattern (basic)
url_pattern = r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+'

# Date pattern (MM/DD/YYYY)
date_pattern = r'\d{2}/\d{2}/\d{4}'

Remember to use raw strings (r'pattern') for regular expressions to avoid issues with backslash escaping!

Similar Posts

  • Vs code

    What is VS Code? 💻 Visual Studio Code (VS Code) is a free, lightweight, and powerful code editor developed by Microsoft. It supports multiple programming languages (Python, JavaScript, Java, etc.) with: VS Code is cross-platform (Windows, macOS, Linux) and widely used for web development, data science, and general programming. 🌐📊✍️ How to Install VS Code…

  • What are Variables

    A program is essentially a set of instructions that tells a computer what to do. Just like a recipe guides a chef, a program guides a computer to perform specific tasks—whether it’s calculating numbers, playing a song, displaying a website, or running a game. Programs are written in programming languages like Python, Java, or C++,…

  • Linear vs. Scalar,Homogeneous vs. Heterogeneous 

    Linear vs. Scalar Data Types in Python In programming, data types can be categorized based on how they store and organize data. Two important classifications are scalar (atomic) types and linear (compound) types. 1. Scalar (Atomic) Data Types 2. Linear (Compound/Sequential) Data Types Key Differences Between Scalar and Linear Data Types Feature Scalar (Atomic) Linear (Compound) Stores Single…

  • Top Programming Languages and Tools Developed Using Python

    Python itself is not typically used to develop other programming languages, as it is a high-level language designed for general-purpose programming. However, Python has been used to create domain-specific languages (DSLs), tools for language development, and educational languages. Here are some examples: 1. Hy 2. Coconut Description: A functional programming language that compiles to Python. It adds…

  • Dynamically Typed vs. Statically Typed Languages 🔄↔️

    Dynamically Typed vs. Statically Typed Languages 🔄↔️ Dynamically Typed Languages 🚀 Python Pros: Cons: Statically Typed Languages 🔒 Java Pros: Cons: Key Differences 🆚 Feature Dynamically Typed Statically Typed Type Checking Runtime Compile-time Variable Types Can change during execution Fixed after declaration Error Detection Runtime exceptions Compile-time failures Speed Slower (runtime checks) Faster (optimized binaries)…

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

Leave a Reply

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