Introduction
Programming में कई बार हमें text data के अंदर किसी specific pattern को ढूंढना पड़ता है।
जैसे किसी sentence में number खोजना, email check करना, mobile number validate करना या किसी word को search करना।
ऐसी situations में Regular Expressions बहुत useful होते हैं।
Regular Expression, जिसे short form में Regex भी कहा जाता है, text processing का एक powerful tool है।
इसकी मदद से हम string में pattern search, match और replace कर सकते हैं।
Regular Expression क्या होता है
Regular Expression एक special pattern होता है, जिसका उपयोग string के अंदर specific format को ढूंढने के लिए किया जाता है।
सरल शब्दों में, यह text searching का advanced तरीका है।
Python में Regular Expressions
Python में Regular Expressions के लिए re module का उपयोग किया जाता है।
import re
यह module हमें कई useful functions देता है, जिनकी मदद से हम text data पर काम कर सकते हैं।
Regular Expressions क्यों जरूरी हैं
- text search करने के लिए
- pattern matching के लिए
- data validation के लिए
- text replace करने के लिए
- large text data को process करने के लिए
Basic Functions of re Module
Python के re module में कुछ important functions होते हैं:
re.search()re.match()re.findall()re.sub()
1. re.search()
यह function string में कहीं भी pattern को search करता है।
अगर pattern मिलता है, तो match object return करता है।
import retext = "Python is easy"
result = re.search("Python", text)
if result:
print("Pattern Found")
Output
Pattern Found
Explanation
यहाँ Python word string में मौजूद है, इसलिए search successful हुआ।
2. re.match()
यह function string की शुरुआत में pattern को check करता है।
import retext = "Python Programming"
result = re.match("Python", text)
if result:
print("Matched at beginning")
Output
Matched at beginning
Explanationmatch() केवल शुरुआत में pattern check करता है।
अगर यही pattern बीच में होता, तो match नहीं होता।
3. re.findall()
यह function string में जितनी बार pattern मिलता है, उन सभी matches की list return करता है।
import retext = "My numbers are 10, 20 and 30"
result = re.findall(r"\d+", text)
print(result)
Output
['10', '20', '30']
Explanation
यहाँ \d+ का मतलब है एक या एक से अधिक digits।
इसलिए string में मौजूद सभी numbers list के रूप में मिल गए।
4. re.sub()
यह function matched text को replace करने के लिए उपयोग होता है।
import retext = "Python is easy"
new_text = re.sub("easy", "powerful", text)
print(new_text)
Output
Python is powerful
Explanation
यहाँ easy word को powerful से replace किया गया है।
Common Regex Symbols
कुछ symbols Regular Expressions में बहुत important होते हैं:
| Symbol | Meaning |
|---|---|
. | कोई भी single character |
\d | कोई digit (0-9) |
\D | non-digit |
\w | word character |
\W | non-word character |
\s | whitespace |
\S | non-whitespace |
+ | one or more times |
* | zero or more times |
^ | start of string |
$ | end of string |
Example: Digits ढूंढना
import re
text = "Room number is 205"
result = re.search(r"\d+", text)
if result:
print(result.group())
Output
205
Example: Word search
import re
text = "I like Python"
result = re.search("Python", text)
if result:
print("Word found")
Data Validation में use
Regular Expressions का सबसे common use validation में होता है।
जैसे:
- email validation
- mobile number validation
- password pattern check
Real-life Example: Email Validation
import re
email = "test@gmail.com"
pattern = r"\S+@\S+\.\S+"
if re.match(pattern, email):
print("Valid Email")
else:
print("Invalid Email")
Explanation
यह example check करता है कि email basic format में है या नहीं।
Real-life Example: Mobile Number Check
import remobile = "9876543210"
pattern = r"^\d{10}$"
if re.match(pattern, mobile):
print("Valid Mobile Number")
else:
print("Invalid Mobile Number")
Explanation
यहाँ pattern check करता है कि input exactly 10 digits का है या नहीं।
Real-life Example: Find all words starting with P
import re
text = "Python is Powerful and Popular"
result = re.findall(r"\bP\w+", text)
print(result)
Output
['Python', 'Powerful', 'Popular']
Important Points
- Regex pattern matching के लिए use होता है
remodule import करना जरूरी है- search, match, findall, sub सबसे common functions हैं
- validation और text processing में इसका बहुत use होता है
Common mistakes
remodule import करना भूल जाना- pattern गलत लिखना
match()औरsearch()में confusion- special symbols का सही meaning न समझना
Mini Program
import re
text = "My roll number is 1234"
result = re.findall(r"\d+", text)
print(result)
Output
['1234']
Summary
Regular Expressions Python में text data के साथ काम करने का एक advanced और powerful तरीका है।
इसकी मदद से हम pattern search, validation और replacement जैसे tasks आसानी से कर सकते हैं।
यह topic practical programming और real-world applications दोनों के लिए बहुत important है।