PHP Basics · Chapter 19 · Regular Expressions

PHP Regular Expressions
Complete Guide in Hindi

PHP Regex की पूरी जानकारी — Pattern syntax, preg_match, preg_replace, preg_split, preg_grep। Email, mobile, URL, password validation। Real examples के साथ।

🔍 preg_match 🔄 preg_replace ✂️ preg_split 📋 Common Patterns 🛡️ Validation
/pattern/Regex delimiter
preg_PHP regex functions
()Capture groups
i m sRegex flags

📋 इस Article में क्या-क्या है

  1. Regex क्या है?
  2. Pattern Syntax — Characters
  3. Quantifiers
  4. Anchors & Groups
  5. Flags (Modifiers)
  6. preg_match()
  7. preg_match_all()
  8. preg_replace()
  9. preg_split() & preg_grep()
  10. Common Patterns — Real World
1
Regex क्या है?

Regular Expression (Regex) — एक pattern जो strings में search, validate, और replace करता है। Email check करना, phone number validate करना, HTML tags हटाना — सब regex से।

// Pattern structure:
/^[a-z]+@[a-z]+\.[a-z]{2,}$/i

// Delimiter: / ... /
// Anchor: ^ = start, $ = end
// Char class: [a-z] = a से z कोई भी
// Quantifier: + = एक या ज़्यादा, {2,} = 2+
// Flag: i = case insensitive
PHP में regex हमेशा /pattern/flags format में — forward slash delimiters ज़रूरी।

2
Pattern Syntax — Characters और Character Classes
PatternमतलबExample Match
.कोई भी एक character (newline छोड़)a, b, 1, @
\dDigit [0-9]0, 5, 9
\DNon-digita, !, space
\wWord char [a-zA-Z0-9_]a, Z, 5, _
\WNon-word char@, !, space
\sWhitespace (space, tab, newline)space, \t
\SNon-whitespacea, 1, @
[abc]a, b, या c में से एकa, b, c
[^abc]a, b, c को छोड़कर कोई भीd, e, 1
[a-z]a से z कोई भीa, m, z
[0-9]0 से 9 कोई भी0, 5, 9
[a-zA-Z]कोई भी lettera, Z, m
\.Literal dot (escape).
a|ba या b (alternation)cat, bat
CHARACTER CLASSES — EXAMPLES
<?php
// \d — digits
preg_match("/\d+/", "Order 12345", $m);
echo $m[0]; // 12345

// \w — word characters
preg_match("/\w+/", "Hello World", $m);
echo $m[0]; // Hello

// [aeiou] — vowels
preg_match_all("/[aeiou]/i", "Hello World", $m);
print_r($m[0]); // [e, o, o]

// [^0-9] — non-digits
echo preg_replace("/[^0-9]/", "", "Phone: 9876543210");
// 9876543210
?>

3
Quantifiers — कितनी बार?
QuantifierमतलबExample
*0 या ज़्यादा बारab* → a, ab, abb, abbb
+1 या ज़्यादा बारab+ → ab, abb (not a)
?0 या 1 बार (optional)colou?r → color, colour
{n}exactly n बार\d{4} → 2025
{n,}n या ज़्यादा बार\d{2,} → 12, 123, 1234
{n,m}n से m बार\d{2,4} → 12, 123, 1234
*?Lazy — minimum match<.+?> → <b> not <b>text</b>
+?Lazy one or moreStops at first match
QUANTIFIERS — GREEDY vs LAZY
<?php
$html = "<b>Bold</b> and <i>Italic</i>";

// Greedy — maximum match (default)
preg_match("'<.+>'", $html, $m);
echo $m[0]; // <b>Bold</b> and <i>Italic</i> — all!

// Lazy — minimum match (? add करो)
preg_match("'<.+?>'", $html, $m);
echo $m[0]; // <b> — first tag only

// {n} exact count
preg_match("/\d{6}/", "PIN: 110001", $m);
echo $m[0]; // 110001
?>

4
Anchors और Groups — Position और Capture
SymbolमतलबExample
^String का start^Hello → "Hello World" ✅
$String का endWorld$ → "Hello World" ✅
\bWord boundary\bcat\b → "cat" not "catch"
(abc)Capture group(ab)+ → ab, abab
(?:abc)Non-capture groupGroup but don't capture
(?P<name>)Named capture group(?P<year>\d{4})
\1 \2Backreference(abc)\1 → abcabc
(?=abc)Lookaheadfoo(?=bar) → foobar
(?!abc)Negative lookaheadfoo(?!bar) → fooXXX
ANCHORS & GROUPS — EXAMPLES
<?php
// ^ $ — String boundaries
var_dump(preg_match("/^Hello/", "Hello World")); // 1 (true)
var_dump(preg_match("/^World/", "Hello World")); // 0 (false)

// Capture groups — () — $matches में save
preg_match("/(\d{4})-(\d{2})-(\d{2})/", "2025-05-15", $m);
echo $m[0]; // 2025-05-15 (full match)
echo $m[1]; // 2025 (group 1)
echo $m[2]; // 05 (group 2)
echo $m[3]; // 15 (group 3)

// Named groups — (?P<name>pattern)
preg_match("/(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})/", "2025-05-15", $m);
echo $m["year"]; // 2025
echo $m["month"]; // 05
echo $m["day"]; // 15

// \b word boundary
preg_match_all("/\bPHP\b/i", "PHP is great. PHPStorm too.", $m);
echo count($m[0]); // 1 — PHP only, not PHPStorm
?>

5
Flags (Modifiers) — Pattern का Behavior Change
FlagमतलबExample
iCase-insensitive/hello/i → Hello, HELLO, hello
mMultiline — ^ $ हर line पर/^\d/m → each line start
sDotall — . newlines भी match करे/a.b/s → "a\nb"
gGlobal (PHP में preg_match_all)All matches find
xExtended — whitespace ignore, commentsReadable patterns
uUnicode — UTF-8 supportHindi/Chinese chars
FLAGS — EXAMPLES
<?php
// i — case insensitive
preg_match("/php/i", "I love PHP"); // 1 — PHP, php, Php सब match

// m — multiline
$text = "line1\nline2\nline3";
preg_match_all("/^line/m", $text, $m);
echo count($m[0]); // 3 — हर line का start

// s — dotall (. newline भी)
preg_match("/start.+end/s", "start\nmiddle\nend"); // 1

// u — unicode (Hindi support)
preg_match("/^\p{L}+$/u", "राहुल"); // 1 — Unicode letters

// x — extended (readable pattern)
$emailPattern = "/ ^ # Start [a-z0-9._-]+ # Local part @ # @ symbol [a-z0-9.-]+ # Domain \. # Dot [a-z]{2,} # TLD $ # End /ix";
?>

6
preg_match() — Pattern Check करना
pmtch

preg_match()

String में pattern match होता है या नहीं — 0 या 1 return करता है। पहला match मिलने पर रुक जाता है। Validation के लिए best।

Returns: 0 or 1 First match only $matches array
int preg_match( string $pattern, string $subject, array &$matches = [] )
preg_match() — VALIDATION EXAMPLES
<?php
// Simple check — match है?
if (preg_match("/^\d{10}$/", "9876543210")) {
  echo "✅ Valid 10-digit number";
}

// $matches — captured groups
if (preg_match("/^(\w+)\s(\w+)$/", "Rahul Kumar", $matches)) {
  echo $matches[0]; // "Rahul Kumar" (full)
  echo $matches[1]; // "Rahul" (first name)
  echo $matches[2]; // "Kumar" (last name)
}

// Date extract
$log = "Error at 2025-05-15 14:30:00: Connection failed";
if (preg_match("/(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})/", $log, $m)) {
  echo "Date: " . $m[1]; // 2025-05-15
  echo "Time: " . $m[2]; // 14:30:00
}
?>

7
preg_match_all() — सभी Matches ढूँढना
pma
all

preg_match_all()

String में pattern के सभी matches ढूँढता है। Email extract करना, prices find करना, links scrape करना — सब matches चाहिए तो यह।

Returns: count ALL matches 2D array
preg_match_all() — EXAMPLES
<?php
// सब email addresses extract करो
$text = "Contact us at rahul@gmail.com or support@hnp.in for help.";
$count = preg_match_all("/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i", $text, $matches);
echo "Found $count emails:\n";
print_r($matches[0]); // [rahul@gmail.com, support@hnp.in]

// All prices extract
$prices = "Items: ₹450, ₹799, ₹299, ₹1,299";
preg_match_all("/₹([\d,]+)/", $prices, $m);
print_r($m[1]); // [450, 799, 299, 1,299]

// HTML links extract
$html = '<a href="https://php.net">PHP</a> <a href="https://hnp.in">HNP</a>';
preg_match_all('/href="([^"]+)"/', $html, $m);
print_r($m[1]); // [https://php.net, https://hnp.in]

// Hashtags extract
$post = "Learning #PHP and #MySQL! #HindiNotes";
preg_match_all("/\#(\w+)/", $post, $m);
print_r($m[1]); // [PHP, MySQL, HindiNotes]
?>

8
preg_replace() — Pattern को Replace करना
prpl
ace

preg_replace() / preg_replace_callback()

String में pattern match होने पर replace करता है। preg_replace_callback() — complex replacement के लिए callback function।

Returns: string $1 $2 backreference callback support
preg_replace() — EXAMPLES
<?php
// Simple replace
echo preg_replace("/PHP/i", "Python", "I love PHP!");
// I love Python!

// Multiple spaces → single
echo preg_replace("/\s+/", " ", "Hello World PHP");
// Hello World PHP

// HTML tags remove
echo preg_replace("'<[^>]+>'", "", "<b>Bold</b> text");
// Bold text

// Backreferences — $1, $2
// Date format change: 2025-05-15 → 15/05/2025
echo preg_replace("/(\d{4})-(\d{2})-(\d{2})/", "$3/$2/$1", "2025-05-15");
// 15/05/2025

// Mask phone number: 9876543210 → 98XXXXXX10
echo preg_replace("/(\d{2})\d{6}(\d{2})/", "$1XXXXXX$2", "9876543210");
// 98XXXXXX10

// preg_replace_callback — complex logic
$text = "price: 100, 200, 300";
echo preg_replace_callback("/\d+/", fn($m) => $m[0] * 1.18, $text);
// price: 118, 236, 354 (18% GST add)
?>

9
preg_split() और preg_grep()
preg_split() — Pattern से Split करना
<?php
// preg_split — pattern से split (explode का powerful version)
// Multiple delimiters — comma, semicolon, pipe
$str = "PHP, MySQL; Laravel | React";
$parts = preg_split("/[,;|]\s*/", $str);
print_r($parts); // [PHP, MySQL, Laravel, React]

// Whitespace split
$words = preg_split("/\s+/", "Hello World PHP");
print_r($words); // [Hello, World, PHP]

// CamelCase को words में split
$camel = "camelCaseVariableName";
$words = preg_split("/(?=[A-Z])/", $camel);
print_r($words); // [camel, Case, Variable, Name]

// preg_grep — array filter with regex
$emails = ["r@gmail.com", "invalid", "p@yahoo.in", "notanemail"];
$valid = preg_grep("/^[a-z0-9.]+@[a-z0-9]+\.[a-z]{2,}$/i", $emails);
print_r($valid); // [r@gmail.com, p@yahoo.in]

// preg_grep — inverse (match नहीं करने वाले)
$invalid = preg_grep("/^[a-z0-9.]+@[a-z0-9]+\.[a-z]{2,}$/i", $emails, PREG_GREP_INVERT);
print_r($invalid); // [invalid, notanemail]
?>

10
Common Patterns — Real World Validation
COMMON REGEX PATTERNS — Ready to Use
<?php
// Email
$emailPattern = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/";
var_dump(preg_match($emailPattern, "rahul@gmail.com")); // 1

// Indian Mobile — 6-9 से शुरू, 10 digits
$mobilePattern = "/^[6-9]\d{9}$/";
var_dump(preg_match($mobilePattern, "9876543210")); // 1

// Indian PIN code — 6 digits, 1-9 से शुरू
$pinPattern = "/^[1-9]\d{5}$/";
var_dump(preg_match($pinPattern, "110001")); // 1

// URL
$urlPattern = "/^https?:\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,}(\/.*)?$/i";
var_dump(preg_match($urlPattern, "https://hindinotespoint.in")); // 1

// Password — 8+, uppercase, lowercase, digit
$passPattern = "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/";
var_dump(preg_match($passPattern, "MyPass123")); // 1
var_dump(preg_match($passPattern, "weakpass")); // 0

// GST Number — 15 chars: 2digit state + 10 PAN + 1Z + 2alphanumeric
$gstPattern = "/^\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}Z[A-Z\d]{1}$/";
var_dump(preg_match($gstPattern, "29ABCDE1234F1ZS")); // 1

// Aadhaar — 12 digits (not starting with 0 or 1)
$aadhaarPattern = "/^[2-9]\d{11}$/";
var_dump(preg_match($aadhaarPattern, "234567891234")); // 1

// Slug — URL-safe lowercase
$slugPattern = "/^[a-z0-9]+(?:-[a-z0-9]+)*$/";
var_dump(preg_match($slugPattern, "php-tutorial-hindi")); // 1

// Slug बनाना from title
function makeSlug(string $title): string {
  $slug = strtolower(trim($title));
  $slug = preg_replace("/[^a-z0-9\s-]/", "", $slug); // Non-alphanumeric remove
  $slug = preg_replace("/\s+/", "-", $slug); // Spaces → dash
  return preg_replace("/--+/", "-", $slug); // Multiple dashes → one
}
echo makeSlug("PHP Tutorial for Beginners!");
// php-tutorial-for-beginners
?>
Pattern याद रखो: ^ = start, $ = end, \d = digit, \w = word, + = 1+, * = 0+, ? = optional, {n} = exact, () = group, [] = character class

Quick Reference — preg_ Functions
FunctionकामReturnsUse Case
preg_match()Pattern match check0 or 1Validation
preg_match_all()All matches findint (count)Extract all emails/links
preg_replace()Pattern replacestringFormat change, clean
preg_replace_callback()Custom replacement logicstringComplex transforms
preg_split()Pattern से splitarrayMultiple delimiters
preg_grep()Array filter by patternarrayFilter valid emails
preg_quote()Special chars escapestringUser input in pattern
preg_quote() — User Input Safe करना
<?php
// User input को pattern में safe use करना
$search = "C++ (language)"; // Special chars!
$escaped = preg_quote($search, "/"); // C\+\+ \(language\)
$pattern = "/" . $escaped . "/i";
preg_match($pattern, "I love C++ (language)"); // ✅ safe
?>

निष्कर्ष

Regular Expressions PHP का powerful feature है। एक बार सीखने के बाद validation, text processing, और data extraction बहुत आसान हो जाती है।

preg_match — validate (0/1)। preg_match_all — extract all। preg_replace — transform।

^ $ — String boundaries। Validation में हमेशा use करो।

() — Capture groups। $matches[1] से access। Named: (?P<name>)।

Greedy vs Lazy — .+ greedy (max), .+? lazy (min)। HTML parse में lazy use करो।

preg_quote() — User input को pattern में use करने से पहले escape करो।

filter_var(FILTER_VALIDATE_EMAIL) — Email के लिए regex से बेहतर।

🚀 अगला Chapter: Chapter 20: PHP Math Functions — abs, ceil, floor, round, pow, sqrt, rand, number_format और ज़्यादा।