🟡 Advanced Python · Lesson 23
Regular Expressions with re Module
re Module के साथ Regular Expressions
What is a Regular Expression?
A regular expression (regex) is a pattern used to search, match or extract text. Python's re module powers it. Regex is great for checking emails, phone numbers, PIN codes and more.
Common Patterns
| Pattern | Matches |
|---|---|
| \d | any digit (0-9) |
| \w | letter, digit or underscore |
| \s | a space |
| . | any character |
| + | one or more |
| * | zero or more |
| {n} | exactly n times |
re Functions
re.search()— find the first match anywhere.re.findall()— return all matches as a list.re.match()— match only at the start.re.sub()— replace matches.
Program 1: Find a Pattern
Python – find.py
import re
text = "My roll number is 42"
match = re.search(r"\d+", text)
if match:
print("Found number:", match.group())Found number: 42
r"\d+" means "one or more digits". .group() returns the matched text.
Program 2: Validate an Email
Python – email.py
import re
pattern = r"^[\w.]+@[\w]+\.[a-z]{2,}$"
email = input("Enter email: ")
if re.match(pattern, email):
print("Valid email")
else:
print("Invalid email")Enter email: aman@gmail.com
Valid email
[\w.]+= name part;@literal;[\w]+= domain.\.[a-z]{2,}= a dot then at least 2 letters (com, in...).
Program 3: Extract All Phone Numbers
Python – phones.py
import re
text = "Call 9876543210 or 9123456780 today"
numbers = re.findall(r"\d{10}", text)
print("Phone numbers:", numbers)Phone numbers: ['9876543210', '9123456780']
\d{10} matches exactly 10 digits; findall returns every match as a list.
Common Mistakes
- Forgetting the
r"..."raw string — backslashes get misread. - Using
match(start only) when you meantsearch(anywhere). - Over-complicating patterns — start simple and test.
Practice Tasks
- Check if a string contains any digit.
- Validate a 6-digit PIN code.
- Extract all words starting with a capital letter.
- Replace all spaces in a string with hyphens using
re.sub.
Summary
- Regex matches text patterns using the
remodule. - Key patterns:
\ddigit,\wword char,+one-or-more,{n}exactly n. - Functions: search, findall, match, sub. Always use raw strings
r"...".
Regular Expression क्या है?
Regular expression (regex) एक pattern है जो text को search, match या extract करने के लिए use होता है। Python का re module इसे चलाता है। Regex emails, phone numbers, PIN codes आदि check करने के लिए बढ़िया है।
Common Patterns
| Pattern | Matches |
|---|---|
| \d | कोई digit (0-9) |
| \w | letter, digit या underscore |
| \s | एक space |
| . | कोई भी character |
| + | एक या ज़्यादा |
| * | शून्य या ज़्यादा |
| {n} | ठीक n बार |
re Functions
re.search()— कहीं भी पहला match ढूंढे।re.findall()— सभी matches list में लौटाए।re.match()— सिर्फ शुरुआत में match।re.sub()— matches replace करे।
Program 1: Pattern ढूंढें
Python – find.py
import re
text = "My roll number is 42"
match = re.search(r"\d+", text)
if match:
print("Found number:", match.group())Found number: 42
r"\d+" मतलब "एक या ज़्यादा digits"। .group() matched text लौटाता है।
Program 2: Email Validate करें
Python – email.py
import re
pattern = r"^[\w.]+@[\w]+\.[a-z]{2,}$"
email = input("Enter email: ")
if re.match(pattern, email):
print("Valid email")
else:
print("Invalid email")Enter email: aman@gmail.com
Valid email
[\w.]+= name part;@literal;[\w]+= domain।\.[a-z]{2,}= एक dot फिर कम से कम 2 letters (com, in...)।
Program 3: सभी Phone Numbers निकालें
Python – phones.py
import re
text = "Call 9876543210 or 9123456780 today"
numbers = re.findall(r"\d{10}", text)
print("Phone numbers:", numbers)Phone numbers: ['9876543210', '9123456780']
\d{10} ठीक 10 digits match करता है; findall हर match list में लौटाता है।
सामान्य गलतियाँ
r"..."raw string भूलना — backslashes गलत पढ़े जाते हैं।search(कहीं भी) की जगहmatch(सिर्फ शुरू) use करना।- Patterns को बहुत जटिल बनाना — simple से शुरू करके test करें।
Practice Tasks
- Check करें कि string में कोई digit है या नहीं।
- 6-digit PIN code validate करें।
- बड़े अक्षर से शुरू होने वाले सभी words निकालें।
re.subसे string के सभी spaces को hyphens से replace करें।
सारांश
- Regex
remodule से text patterns match करता है। - मुख्य patterns:
\ddigit,\wword char,+एक-या-ज़्यादा,{n}ठीक n। - Functions: search, findall, match, sub। हमेशा raw strings
r"..."use करें।