🧬 Regex for Advanced Filtering
Last Updated: January 2026
Regex allows pattern-based filtering when simple conditions are not enough. It is commonly used with:
- REGEXP / RLIKE (MySQL)
- ~ operator (PostgreSQL)
Hinglish Tip 🗣: Regex tab use hota hai jab filter rule-based pattern par depend karta ho.
Basic Usage
SELECT column_name
FROM table_name
WHERE column_name REGEXP 'pattern';
🔤 Core Regex Symbols
🔍 SQL Examples
^ — Starts With
SELECT name
FROM students
WHERE name REGEXP '^A';
$ — Ends With
SELECT name
FROM students
WHERE name REGEXP 'n$';
| — OR
SELECT name
FROM students
WHERE name REGEXP 'Ram|Shyam';
. — Any One Character
SELECT code
FROM products
WHERE code REGEXP 'A.B';
Matches: A1B, ACB, A-B
* — Zero or More
SELECT value
FROM logs
WHERE value REGEXP 'ab*';
Matches: a, ab, abb
[] — Character Set
SELECT name
FROM students
WHERE name REGEXP '^[A-Z]';
+ — One or More
SELECT name
FROM users
WHERE name REGEXP 'a+';
? — Zero or One
SELECT color
FROM items
WHERE color REGEXP 'colou?r';
Matches: color, colour
{m} — Exact Count
SELECT pin
FROM addresses
WHERE pin REGEXP '^[0-9]{6}$';
{m, or } — Minimum Count
SELECT username
FROM users
WHERE username REGEXP '[a-z]{5,}';
{n, m} — Range Count
SELECT code
FROM products
WHERE code REGEXP '[A-Z]{2,4}';
⚠️ Important Notes
- Regex support varies slightly by database
- Always test patterns before using in production
- Use anchors (^, $) to avoid partial matches
