←

Python Regular Expressions (REGEX) Notes

Master pattern searching, matching, data validation, extraction, and string cleaning in Python using the built-in re module.

Author: By Instructor

1. What is a Regular Expression (Regex)?

A Regular Expression (Regex) is a sequence of characters that forms a search pattern. It is used to check if a string contains a specified search pattern, validate form inputs, or extract data.

Easy English Explanation

  • Pattern Matching: Technique to find, extract, or validate text data.
  • Built-in Module: Python includes the re module for regex operations.
  • Versatility: Used across web forms, security logs, data science, and text processing.

Hinglish Explanation

  • Regex Ka Matlab: Text ke andar pattern dhundhne, validate karne ya extract karne ka tarika.
  • Python Module: Python mein Regex use karne ke liye hum built-in re module import karte hain.
  • Fayde: Validation (email, phone, password), data extraction aur string cleaning dynamic ho jati hai.
import re

2. Core Regex Functions: re.search(), re.match(), re.findall()

Function Differences

  • re.search(): Searches anywhere in the string for a match.
  • re.match(): Matches ONLY at the beginning/start of the string.
  • re.findall(): Finds ALL occurrences and returns them as a List of strings.
  • re.fullmatch(): Returns match object ONLY if the ENTIRE string matches the pattern.

Functions ka Difference

  • re.search(): Poori string me kahi bhi pattern dhundhta hai.
  • re.match(): Sirf string ke START me match check karta hai.
  • re.findall(): Saare matches ki ek List return karta hai.
  • re.fullmatch(): Jab poori string pattern ke a-to-z according ho tabhi True deta hai.
Python Code - search, match, findall, fullmatch
import re

text = "My name is Gagan"

search_res = re.search("Gagan", text)
print("Search Result:", search_res)

match_res = re.match("Gagan", text)
print("Match Result (Starts with Gagan?):", match_res)

text2 = "apple mango apple banana apple"
all_apples = re.findall("apple", text2)
print("Findall Result:", all_apples)
Output
Search Result: <re.Match object; span=(11, 16), match='Gagan'>
Match Result (Starts with Gagan?): None
Findall Result: ['apple', 'apple', 'apple']

3. Text Modification: re.sub() and re.split()

Python Code - sub and split
import re

text1 = "I like Java"
replaced = re.sub("Java", "Python", text1)
print("Replaced Text:", replaced)

text2 = "apple,mango,banana"
split_list = re.split(",", text2)
print("Split List:", split_list)
Output
Replaced Text: I like Python
Split List: ['apple', 'mango', 'banana']

4. Basic Metacharacters (. * + ?)

Symbol Meaning Example Pattern Matches
. Any single character (except newline) c.t cat, cut, cot, c1t
* Zero or more times co*l cl, col, cool, coooool
+ One or more times (at least once) co+l col, cool, coooool
? Zero or one time (Optional) colou?r color, colour
Python Code - Metacharacters in Action
import re

print("Dot match:", re.search("c.t", "cat").group())
print("Star match:", re.search("co*l", "cooooool").group())
print("Plus match:", re.search("co+l", "coool").group())
print("Question match:", re.search("colou?r", "color").group())
Output
Dot match: cat
Star match: cooooool
Plus match: coool
Question match: color

5. Character Classes & Sets ([abc], [0-9], [a-z], [A-Z])

Square brackets [] define a set of characters to match.

Python Code - Character Sets
import re

text1 = "My age is 22"
digits = re.findall("[0-9]", text1)

text2 = "hello123"
lowers = re.findall("[a-z]", text2)

text3 = "Hello GAGAN"
uppers = re.findall("[A-Z]", text3)

print("Digits:", digits)
print("Lowercase Letters:", lowers)
print("Uppercase Letters:", uppers)
Output
Digits: ['2', '2']
Lowercase Letters: ['h', 'e', 'l', 'l', 'o']
Uppercase Letters: ['H', 'G', 'A', 'G', 'A', 'N']

6. Special Sequences (\d, \D, \w, \W, \s, \S)

Sequence Description Matches
\d Digits (0-9) Any single digit
\D Non-digits Anything except digits
\w Word character Letters, numbers, and underscore (_)
\W Non-word character Symbols (@, !, %, spaces)
\s Whitespace Space, tab, newline
\S Non-whitespace Any non-space character

7. Anchors (^ and $)

Anchor Meaning

  • ^: Asserts position at the START of the string.
  • $: Asserts position at the END of the string.

Anchor ka Matlab

  • ^: Batata hai ki string is text se SHURU honi chahiye.
  • $: Batata hai ki string is text par KHATAM honi chahiye.
Python Code - Anchors
import re

text = "Hello Gagan"
starts_hello = re.search(r"^Hello", text)
ends_gagan = re.search(r"Gagan$", text)

print("Starts with Hello?:", bool(starts_hello))
print("Ends with Gagan?:", bool(ends_gagan))
Output
Starts with Hello?: True
Ends with Gagan?: True

8. Quantifiers ({n}, {n,m})

Python Code - Quantifiers
import re

text = "My number is 12345"
exact_5 = re.findall(r"\d{5}", text)

text2 = "123456789"
range_2_4 = re.findall(r"\d{2,4}", text2)

print("Exact 5 digits:", exact_5)
print("Range 2 to 4 digits:", range_2_4)
Output
Exact 5 digits: ['12345']
Range 2 to 4 digits: ['1234', '5678', '9']

9. Form Validation: Phone, Email & Password

Python Code - Validation Patterns
import re

phone = "9876543210"
phone_pattern = r"^\d{10}$"
is_valid_phone = bool(re.fullmatch(phone_pattern, phone))

email = "gagan@gmail.com"
email_pattern = r"^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$"
is_valid_email = bool(re.fullmatch(email_pattern, email))

password = "Gagan123"
pass_pattern = r"^(?=.*[A-Za-z])(?=.*\d).{6,}$"
is_valid_pass = bool(re.fullmatch(pass_pattern, password))

print("Valid Phone?:", is_valid_phone)
print("Valid Email?:", is_valid_email)
print("Valid Password?:", is_valid_pass)
Output
Valid Phone?: True
Valid Email?: True
Valid Password?: True

Email Pattern Breakdown

Part Pattern Snippet Explanation
Start ^ Beginning of email string
Username [\w.-]+ Letters, digits, underscores, dots, hyphens
Symbol @ Mandatory @ symbol
Domain [\w.-]+ Domain name (e.g. gmail, yahoo)
Dot \. Literal dot (.)
Extension [a-zA-Z]{2,} Minimum 2 letters (.com, .in, .org)
End $ End of email string

10. Practical Data Extraction & Text Cleaning

Python Code - Extracting Phones, Emails, Mentions, Cleaning
import re

text = "Invoice INV1001 for Gagan
Contact: 9876543210 or email gagan@gmail.com
Mentions: @gagan and @rajat"

inv_num = re.findall(r"INV\d+", text)
phones = re.findall(r"\d{10}", text)
emails = re.findall(r"[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}", text)
mentions = re.findall(r"@\w+", text)

raw_messy = "Hello     Gagan    How   are  you"
clean_spaces = re.sub(r"\s+", " ", raw_messy)

print("Invoice:", inv_num)
print("Phones:", phones)
print("Emails:", emails)
print("Mentions:", mentions)
print("Clean Spaces:", clean_spaces)
Output
Invoice: ['INV1001']
Phones: ['9876543210']
Emails: ['gagan@gmail.com']
Mentions: ['@gagan', '@rajat']
Clean Spaces: Hello Gagan How are you

11. 20 Real-World Use Cases of Regex

  • 1. Email Validation: Signup & login forms.
  • 2. Phone Number Validation: WhatsApp/Banking registrations.
  • 3. Password Strength Check: Security enforcement in apps.
  • 4. Extracting Phone Numbers: Scraped documents & customer text.
  • 5. Extracting Emails: Customer lead generation.
  • 6. Extracting Numbers from Invoices: Invoice prices and IDs.
  • 7. Data Cleaning: Replacing extra spaces (\s+) in datasets.
  • 8. Special Character Removal: Sanitizing usernames.
  • 9. Pincode/Zip Code Check: Indian 6-digit address verification.
  • 10. Date Format Validation: Checking DD-MM-YYYY format.
  • 11. Username Validation: Alphanumeric + underscore checks.
  • 12. URL Protocol Check: Ensuring http:// or https://.
  • 13. Log File Analysis: Parsing server error codes & IP addresses.
  • 14. Global Text Search: Search engine indexing.
  • 15. Web Scraping Pattern Matching: Extracting price tags from HTML.
  • 16. Mentions Extraction: Social media @mentions parsing.
  • 17. Price Extraction: Extracting numeric prices from product titles.
  • 18. Invoice Data Extraction: Pulling Invoice ID, phone, email, and amount.
  • 19. Banking Reference Validation: Checking transaction IDs.
  • 20. Document Processing: Parsing PDF contents into database fields.

12. The 5 Pillars of Regex & Summary

The 5 Core Pillars

  1. SEARCH: Finding patterns inside text (re.search).
  2. VALIDATE: Checking format compliance (re.fullmatch).
  3. EXTRACT: Pulling specific pieces of data (re.findall).
  4. REPLACE: Swapping target text (re.sub).
  5. CLEAN: Removing unwanted symbols/spaces.

5 Pillars Breakdown

  1. Search: Text me kuch dhundhna.
  2. Validate: Format sahi hai ya nahi check karna.
  3. Extract: Specific data bahar nikalna.
  4. Replace: Text ko change/swap karna.
  5. Clean: Unwanted symbols/spaces hatana.