Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

🟢 Beginner🔵 Advanced
String Basics & CreationString Indexing & SlicingCommon String MethodsString Formatting EssentialsFinding & Replacing TextString Testing & ValidationRegular Expression BasicsText Splitting & JoiningPractical String Projects
Python/String Manipulation/Regex Basics

🎯 Regular Expression Basics — Master Pattern Matching

Regular expressions (regex) let you search, match, and extract text using patterns. They're powerful tools for text processing.


🎨 Introduction to Regex

A regular expression is a pattern that describes text. Use the `re` module to work with regex in Python:

import re

# Simple pattern matching
text = "The phone number is 555-1234."
pattern = r"\d{3}-\d{4}"  # Pattern: 3 digits, dash, 4 digits

match = re.search(pattern, text)
if match:
    print(f"Found: {match.group()}")  # Found: 555-1234

🔑 Basic Metacharacters

Regex uses special characters to define patterns:

import re

# . matches any character except newline
re.search(r"c.t", "cat")      # Match: 'cat'
re.search(r"c.t", "cut")      # Match: 'cut'

# \d matches any digit
re.search(r"\d", "abc123")    # Match: '1'

# \w matches word characters (letters, digits, underscore)
re.search(r"\w+", "hello")    # Match: 'hello'

# \s matches whitespace
re.search(r"\s", "hello world")  # Match: space

# ^ matches start of string
re.search(r"^Hello", "Hello World")  # Match

# $ matches end of string
re.search(r"World$", "Hello World")  # Match

💡 Quantifiers

Control how many times a pattern repeats:

import re

# * means 0 or more
re.findall(r"a*", "aaa baa")    # ['aaa', '', 'aa', '']

# + means 1 or more
re.findall(r"a+", "aaa baa")    # ['aaa', 'aa']

# ? means 0 or 1
re.findall(r"colou?r", "color colour")  # ['color', 'colour']

# {n} means exactly n
re.findall(r"\d{3}", "123 45 678")      # ['123', '678']

# {n,m} means n to m
re.findall(r"\d{2,3}", "12 345 6789")   # ['12', '345', '67']

📊 Common Regex Patterns

PatternMeaningExample
`.`Any character`a.c` matches "abc", "adc"
`\d`Any digit`\d+` matches "123"
`\w`Word character`\w+` matches "hello"
`\s`Whitespace`\s+` matches spaces/tabs
`^`Start of string`^hello` matches "hello world"
`$`End of string`world$` matches "hello world"
`[abc]`Any of a, b, c`[aeiou]` matches vowels
`[^abc]`Not a, b, or c`[^0-9]` matches non-digits

🎯 Common Regex Functions

import re

text = "My email is john@example.com"

# search() - find first match
match = re.search(r"\w+@\w+\.\w+", text)
if match:
    print(match.group())  # john@example.com

# findall() - find all matches
text = "Call me: 555-1234 or 555-5678"
numbers = re.findall(r"\d{3}-\d{4}", text)
print(numbers)  # ['555-1234', '555-5678']

# match() - check if string starts with pattern
if re.match(r"My", text):
    print("Text starts with 'My'")

# sub() - replace patterns
result = re.sub(r"\d", "X", "Call 555-1234")
print(result)  # Call XXX-XXXX

💡 Real-World Example: Email Validation

import re

def is_valid_email(email):
    """Check if email matches basic pattern"""
    pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
    return re.match(pattern, email) is not None

print(is_valid_email("john@example.com"))    # True
print(is_valid_email("invalid.email"))       # False
print(is_valid_email("user+tag@site.org"))   # True

🎨 Extracting Data with Groups

import re

text = "Date: 2026-02-20"

# Parentheses create groups
match = re.search(r"(\d{4})-(\d{2})-(\d{2})", text)
if match:
    year, month, day = match.groups()
    print(f"Year: {year}, Month: {month}, Day: {day}")
    # Year: 2026, Month: 02, Day: 20

🔑 Key Takeaways

ConceptRemember
Import re moduleAlways start with `import re`
Use raw stringsUse `r""` for patterns to avoid escape issues
search() vs match()`search()` finds anywhere; `match()` checks start
Metacharacters matter`.` `\d` `\w` `\s` `^` `$` are essential
Quantifiers control`*` `+` `?` `{n,m}` specify repetition

🔗 What's Next?

Learn to split and join text using string operations.


Ready to practice? Challenges | Quiz


Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep • Build Real • Verify Skills • Launch Forward

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn