Python me Regular Expressions (RegEx) kya hai?


Regular Expression (RegEx) ek pattern hai jo strings me specific text dhundhne, check karne ya replace karne ke kaam aata hai. Jaise email validate karna, phone number check karna — yeh sab RegEx se bahut aasaan ho jaata hai. Python me re module use hota hai.

Is page me yeh sab sikhenge :-



1. re Module aur Basic Functions

FunctionKaam
re.match(pat, str)String ke shuru me pattern dhundho
re.search(pat, str)String ke kahi bhi pattern dhundho
re.findall(pat, str)Saare matches ki list do
re.sub(pat, repl, str)Pattern ko replace karo
re.split(pat, str)Pattern pe split karo
re.compile(pat)Pattern ko compile karo

Example 1: re.search() — basic pattern dhundhna

Python
In [1]:
1
Output


2. Meta Characters — Special Symbols

SymbolMatlabExample
.Koi bhi ek charactera.c → abc, aXc
^String ka shuru^Hello
$String ka antworld$
*0 ya zyada baarab*
+1 ya zyada baarab+
?Optional (0 ya 1 baar)colou?r
{n,m}n se m baar\d{2,4}
\dDigit (0-9)\d+
\wWord character\w+
\sWhitespace\s+
[]Character class[aeiou]
()Group(\d{2})

Example 1: \d, \w, \s meta characters

Python
In [1]:
1
Output


3. re.search() vs re.match()

re.match() sirf string ke bilkul shuru me dhundhta hai. re.search() string me kahin bhi dhundhta hai.

Example 1: match vs search — fark samjho

Python
In [1]:
1
Output


4. re.findall() — Saare Matches Nikalna

Example 1: Emails aur phone numbers nikalna

Python
In [1]:
1
Output

Example 2: Dates aur hashtags dhundhna

Python
In [1]:
1
Output


5. re.sub() — Replace Karna

Example 1: Text clean karna

Python
In [1]:
1
Output


6. re.split() — Pattern Pe Split Karna

Example 1: Multiple separators pe split

Python
In [1]:
1
Output


7. Groups aur Practical Examples

Example 1: Groups — date ke parts alag nikalna

Python
In [1]:
1
Output

Example 2: Email validation

Python
In [1]:
1
Output

Example 3: Password strength check

Python
In [1]:
1
Output



Quick Summary — RegEx ek nazar me

Function / SymbolKaam
re.search(pat, s)Kahi bhi dhundho
re.match(pat, s)Sirf shuru me dhundho
re.findall(pat, s)Saare matches ki list
re.sub(pat, r, s)Replace karo
re.split(pat, s)Split karo
\d, \w, \sDigit, Word, Whitespace
+, *, ?1+, 0+, optional
^, $Start, End
[], ()Character set, Group



🎉🐍🎊

Aap sabne ye notes padha — isliye aapne muje shukriya kehena chahiye! 🤣🤣🤣🤣

Agar aapne yahan tak padd liya toh samjho basics to clear hai — lekin ek baat yaad rakhna —

😜😜 Job lene ke liye itna kaafi nahi hai — toh padte raho! 😜😜

Ab aage kya seekhein? 👇

📦 NumPy & Pandas 🌐 Flask / Django 🤖 Machine Learning 🗄️ SQL / Databases 🧪 DSA 🔧 Projects banana ☁️ APIs & Cloud

— Codepth Team  |  © 2026 Codepth  |  Padte raho, badhte raho! 🚀