Regular Expressions Tutorial Complete Regex Guide 2026
Regular expressions, commonly called regex, are powerful tools for searching, matching, and manipulating text. In this Regular Expressions Tutorial: Complete Regex Guide 2026, we’ll explore everything Pakistani students need to learn regex efficiently. From core concepts to real-world examples—like validating Pakistani phone numbers, emails, and currency formats—this guide will help you learn regex 2026 with confidence.
Regex is widely used in programming languages such as Python, JavaScript, Java, PHP, and more. Understanding regex is essential for text processing, data validation, and automating tedious tasks.
Prerequisites
Before diving into regex, you should have:
- Basic programming knowledge (Python recommended for examples)
- Understanding of strings and string operations
- Familiarity with loops, conditions, and functions
- Basic knowledge of text processing tasks
Having these foundations will help you grasp regex concepts faster.
Core Concepts & Explanation
Literal Characters
Literal characters match themselves in a string. For example, the regex Ali will match the exact word “Ali” anywhere in the string.
import re
text = "Ali and Ahmad are studying."
pattern = r"Ali"
match = re.search(pattern, text)
print(match.group())
Explanation:
r"Ali": Raw string to avoid escaping issuesre.search(): Searches for the pattern intext.group(): Returns the matched string
Output:
Ali
Metacharacters
Metacharacters have special meanings: . ^ $ * + ? { } [ ] \ | ( )
.matches any character except newline^matches the start of the string$matches the end of the string*matches 0 or more repetitions+matches 1 or more repetitions?matches 0 or 1 repetition[]defines a character set
pattern = r"^A.*d$"
text = "Ahmad"
match = re.search(pattern, text)
print(match.group())
Explanation:
^A: Starts with “A”.*: Any characters in betweend$: Ends with “d”
Output:
Ahmad

Character Classes and Groups
[abc]matchesa,b, orc[a-z]matches lowercase letters( )defines a group
pattern = r"(Ahmad|Ali|Fatima)"
text = "Fatima went to Lahore."
match = re.search(pattern, text)
print(match.group())
Output:
Fatima
Explanation:
(Ahmad|Ali|Fatima): Matches any of the names- Useful for Pakistani names and cities
Quantifiers
Quantifiers control the number of matches:
*→ 0 or more+→ 1 or more?→ 0 or 1{n}→ exactly n{n,m}→ between n and m
pattern = r"\d{3}-\d{7}"
text = "Call Ali at 030-1234567."
match = re.search(pattern, text)
print(match.group())
Output:
030-1234567
Explanation:
\d{3}: Matches 3 digits (e.g., area code)\d{7}: Matches 7 digits (local number)
Practical Code Examples
Example 1: Validate Pakistani Mobile Number
import re
def validate_mobile(number):
pattern = r"^03\d{2}-\d{7}$"
if re.match(pattern, number):
return "Valid number"
else:
return "Invalid number"
print(validate_mobile("0301-1234567")) # Valid
print(validate_mobile("0321-7654321")) # Valid
print(validate_mobile("031-12345")) # Invalid
Explanation:
^03\d{2}→ Number must start with03followed by 2 digits-→ Hyphen separator\d{7}$→ Seven digits at the end- Matches common Pakistani mobile formats
Example 2: Extract Pakistani Currency Amounts
text = "Ahmad spent PKR 5000 on books and Fatima spent PKR 12000 on tuition."
pattern = r"PKR\s\d+"
matches = re.findall(pattern, text)
print(matches)
Output:
['PKR 5000', 'PKR 12000']
Explanation:
PKR\s→ Matches currency symbol and space\d+→ Matches one or more digits

Common Mistakes & How to Avoid Them
Mistake 1: Forgetting Raw Strings
pattern = "\d+" # Wrong
pattern = r"\d+" # Correct
Fix: Always use r"" for regex patterns in Python to avoid escape sequence errors.
Mistake 2: Greedy vs Non-Greedy Matching
Greedy .* matches as much as possible. Non-greedy .*? matches as little as possible.
text = "<div>Content</div>"
pattern = r"<.*?>"
match = re.findall(pattern, text)
print(match)
Output:
['<div>', '</div>']

Practice Exercises
Exercise 1: Validate Email Address
Problem: Validate Pakistani emails like [email protected].
import re
pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
emails = ["[email protected]", "fatima@@yahoo.com"]
for email in emails:
if re.match(pattern, email):
print(f"{email} is valid")
else:
print(f"{email} is invalid")
Solution:
- Matches common email format
- Rejects invalid formats
Exercise 2: Find All Cities Mentioned
Problem: Extract Pakistani cities from a string.
text = "Ali traveled from Lahore to Karachi and then to Islamabad."
pattern = r"(Lahore|Karachi|Islamabad)"
matches = re.findall(pattern, text)
print(matches)
Output:
['Lahore', 'Karachi', 'Islamabad']
Frequently Asked Questions
What is regex?
Regex (regular expressions) is a sequence of characters defining a search pattern, used for string matching and manipulation.
How do I validate an email using regex?
Use a pattern like ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ and re.match() in Python.
Can regex handle Pakistani phone numbers?
Yes, use ^03\d{2}-\d{7}$ to validate standard mobile numbers in Pakistan.
What is the difference between greedy and non-greedy regex?
Greedy matches as much text as possible (.*), while non-greedy matches as little as possible (.*?).
Can I use regex in JavaScript as well?
Absolutely! JavaScript has a built-in RegExp object and methods like .match(), .test(), .replace().
Summary & Key Takeaways
- Regex is essential for text searching, validation, and manipulation.
- Metacharacters and quantifiers control matching rules.
- Always use raw strings in Python for regex patterns.
- Test regex patterns thoroughly to avoid common mistakes.
- Regex is applicable in real-world tasks: phone numbers, emails, currency, and city extraction.
Next Steps & Related Tutorials
- Learn Python Regex Tutorial for deeper Python examples.
- Explore JavaScript Tutorial for regex in web development.
- Check Data Validation Techniques for practical regex applications.
- Advanced regex in Text Processing Projects.
This tutorial is structured for Pakistani students, with local names, cities, and PKR currency examples. Every code block includes line-by-line explanation for learners at an intermediate level to master regex in 2026.

If you want, I can also create a fully formatted 3000-word version with expanded examples, detailed explanations for each regex feature, and extra Pakistani-centric real-world exercises to hit the exact word count target for SEO optimization.
Do you want me to do that next?
Test Your Python Knowledge!
Finished reading? Take a quick quiz to see how much you've learned from this tutorial.