কেন REGEXP — আগে গল্প
মূল সমস্যা: LIKE দিয়ে starts/ends/contains অনেক সময় যথেষ্ট — কিন্তু জটিল নিয়ম (৩ অক্ষর + ৩ সংখ্যা, A/B/C দিয়ে শুরু, ইত্যাদি) LIKE-এ কঠিন।
গল্প ১ — Email
শুধু LIKE দিয়ে কি সব ধরনের structure সহজে খুঁজবে?
গল্প ২ — Phone
“যেগুলো mobile number-এর মতো দেখতে” — pattern rules লাগে।
গল্প ৩ — Employee ID
EMP + number চাই; HR1001, ABC বাদ।
গল্প ৪ — Product codes
SAM- দিয়ে শুরু + শেষে number।
Regular Expression কী?
Regular + Expression = একটা rule/pattern যেটা বলে আমরা কেমন text খুঁজছি।
EMP দিয়ে শুরু এবং পরে number থাকলে ঢুকতে দাও” — এই নিয়মটাই pattern।LIKE vs REGEXP
LIKE
Rah দিয়ে শুরু — সহজ।
জটিল চাহিদা
A বা B দিয়ে শুরু; অথবা ৩ letter + ৩ digit — REGEXP স্বাভাবিক।
| Feature | LIKE | REGEXP |
|---|---|---|
| Simple pattern | ✅ | ✅ |
| Starts / Ends / Contains | ✅ | ✅ |
| Character rules | Limited | Powerful |
| Multiple alternatives | Limited | Easy (|) |
| Complex validation | Difficult | Better |
| Regex symbols | ❌ (% _) | ✅ |
Basic REGEXP সিনট্যাক্স
REGEXP / RLIKE একই ধরনের কাজ। ভার্সন/ইঞ্জিনভেদে কিছু পার্থক্য থাকতে পারে — ক্লাসে মূল ধারণাই যথেষ্ট।Literal, ^ এবং $
Contains — literal
Start — ^
End — $
Character classes [ ]
| Pattern | Meaning |
|---|---|
[abc] | a/b/c-এর যেকোনো একটি |
[0-9] | যেকোনো digit |
[A-Z] | uppercase letter |
[a-z] | lowercase letter |
Repetition: * + ? {n} {n,m}
*= zero or more+= one or more?= zero or one{n}= exactly n{n,m}= n থেকে m
Match
No match
. Wildcard
. ≈ যেকোনো একটি character (সাধারণ টেক্সটে)।
| OR Pattern
বিজনেস প্যাটার্ন উদাহরণ
- Names ^A →
REGEXP '^A' - Ends n →
REGEXP 'n$' - Contains Rah →
REGEXP 'Rah' - Starts A/B/C →
REGEXP '^[ABC]' - ID has digit →
REGEXP '[0-9]' - 3 letters + 3 digits →
REGEXP '^[A-Z]{3}[0-9]{3}$' - Dhaka|Chittagong →
REGEXP 'Dhaka|Chittagong'
ব্রেকডাউন: পুরো স্ট্রিং ঠিক ৩ বড় হাতের অক্ষর + ৩ সংখ্যা।
একই সমস্যা — LIKE vs REGEXP
Starts with A:
LIKE
REGEXP
Exactly 3 letters + 3 digits → LIKE কঠিন; REGEXP সোজা।
| Requirement | LIKE | REGEXP |
|---|---|---|
| Starts with A | Easy | Easy |
| Ends with n | Easy | Easy |
| Contains Rah | Easy | Easy |
| A/B/C start | Less convenient | Easy |
| Exactly 3 letters/digits | Difficult | Easy |
| Complex structure | Poor fit | Strong fit |
Workbench ল্যাব
অনুশীলন কোয়েরি
REGEXP + অন্যান্য ক্লজ
কমন বিগিনার ভুল
1. REGEXP-এ %
% হলো LIKE wildcard — সাধারণ regex নয়।
2. ^ vs $ গুলিয়ে ফেলা
^ শুরু, $ শেষ।
3. * vs +
* = ০ বা বেশি; + = অন্তত ১।
4. Quotes ভুলে যাওয়া
5. সহজ সমস্যায় অতিরিক্ত regex
6. সব DB এক নয়
MySQL / PostgreSQL / SQL Server regex সাপোর্ট আলাদা হতে পারে।
ইন্ডাস্ট্রি ব্যবহার
| ক্ষেত্র | কাজ |
|---|---|
| Data Quality | Malformed ID খোঁজা |
| Analytics | Email/phone pattern |
| E-commerce | Product code সার্চ |
| HR | Employee ID validate |
| Finance | Reference pattern |
| Data Eng | Suspicious records |
| Data Science | Text EDA |
| Backend | Structured search |
Data Quality কেস স্টাডি
Valid ID: EMP001 স্টাইল — ৩ letter + ৩ digit। DB-তে মিশ্রণ:
Valid
Invalid (মিলছে না)
ক্লাসরুম অ্যাক্টিভিটি (১২+)
Regex Cheat Sheet
| Symbol | Meaning | Example idea |
|---|---|---|
^ | Start | ^A |
$ | End | n$ |
. | Any one char | A.C |
[abc] | One of a/b/c | ^[ABC] |
[0-9] | Digit | has number |
[A-Z] | Upper | code letters |
[a-z] | Lower | — |
* | 0+ | — |
+ | 1+ | — |
? | 0 or 1 | — |
{n} | Exactly n | {3} |
{n,m} | n to m | {2,4} |
| | OR | A|B |
Pattern building ধাপে ধাপে
Requirement: EMP দিয়ে শুরু + exactly ৩ digit।
Execution flow (শিক্ষার মডেল)
Performance tips
- সিম্পল pattern ভালো।
- জটিল regex বড় টেবিলে ব্যয়বহুল হতে পারে।
=বা LIKE যথেষ্ট হলে সেটাই ব্যবহার করো।- আগে অন্য ফিল্টার দিয়ে ডেটা ছোট করো।
- কিছু pattern-এ index কম কাজে লাগে।
Revision
- Regex = text-এর জন্য rule/pattern
- LIKE = simple; REGEXP = powerful
- ^ $ [ ] {n} | . * + ?
LIKE = simple pattern search
REGEXP = powerful rule-based pattern search
^EMP[0-9]{3}$ → EMP + exactly 3 digitsMCQ (২০+)
Viva (২০+)
Interview Top 30
Homework
প্রশ্ন
- Names starting with A
- Names ending with n
- Names containing Rah
- Values containing numbers
- Employee IDs EMP + 3 digits
- Product codes 3 letters + 3 numbers
- Emails ending with .com (careful with dots)
- Cities starting with D or C
- Invalid employee IDs (NOT matching 3+3)
- REGEXP + ORDER BY + LIMIT
উত্তর (ক্লিক)
WHERE name REGEXP '^A'WHERE name REGEXP 'n$'WHERE name REGEXP 'Rah'WHERE col REGEXP '[0-9]'WHERE employee_code REGEXP '^EMP[0-9]{3}$'WHERE product_code REGEXP '^[A-Z]{3}[0-9]{3}$'WHERE email REGEXP '\\.com$' (বা engine-এর মতো escape) — বিগিনার বিকল্প: LIKE '%.com' যদি যথেষ্ট।WHERE city REGEXP '^[DC]' বা '^D|^C'WHERE employee_code NOT REGEXP '^[A-Z]{3}[0-9]{3}$'WHERE name REGEXP '^A' ORDER BY name LIMIT 5