← Back to PHP Course | Chapter 6: Strings | Lesson 5 of 8

PHP Regular Expressions

एक regular expression text में एक pattern describe करने का एक secret code है, जैसे 'तीन digits फिर एक dash'। यह PHP को उन words find या check करने देता है जो pattern से fit होते हैं, जैसे एक metal detector सिर्फ metal ढूँढता है।
Syntax
php
preg_match("/pattern/", $subject, $matches);       // first match
preg_match_all("/pattern/", $subject, $matches);   // every match
$result = preg_replace("/pattern/", "replacement", $subject);

Basic Match Checking

Regular expressions special metacharacters इस्तेमाल करके एक text pattern describe करते हैं, आपको exact literal value के बजाय एक shape follow करने वाली strings match, extract, या validate करने देते हुए।

PHP के preg_* functions PCRE (Perl-Compatible Regular Expressions) engine को wrap करते हैं, इसलिए वही pattern syntax जो आप Perl या कई दूसरी languages में इस्तेमाल करेंगे यहाँ भी काम करता है।

उदाहरण: Basic Match Checking

php
<?php
// Declare `$pattern`, set to "/^[a-z]+$/"
$pattern = "/^[a-z]+$/";
// Print a detailed dump (with types) of `preg_match($pattern, "hello")`
var_dump(preg_match($pattern, "hello"));
?>

सभी Matches ढूँढना

preg_match() किसी string में किसी pattern की पहली occurrence search करता है और report करता है कि यह मिली या नहीं, optionally matched text और किसी captured groups के साथ एक output array भरते हुए।

यह एक email address या phone number roughly किसी expected shape से match करता है इसे confirm करने जैसे validation checks का workhorse है।

उदाहरण: Finding All Matches

php
<?php
// Declare `$email`, set to "[email protected]"
$email = "[email protected]";
// Check whether `preg_match("/^\S+@\S+\.\S+$/", $email)`
if (preg_match("/^\S+@\S+\.\S+$/", $email)) {
    // Print "Valid email format" to the output
    echo "Valid email format";
}
?>

Patterns के साथ Text Replace करना

preg_match_all() preg_match() जैसा behave करता है लेकिन पहले hit के बाद भी scan करता रहता है, पहले पर रुकने के बजाय string में हर match return करते हुए।

इसे तब इस्तेमाल करें जब आपको किसी pattern की सभी occurrences निकालनी हों, जैसे text के किसी block में हर hashtag या URL।

उदाहरण: Replacing Text with Patterns

php
<?php
// Declare `$text`, set to "Contact #alice and #bob for details"
$text = "Contact #alice and #bob for details";
// Call `preg_match_all("/#(\w+)/", $text, $matches)`
preg_match_all("/#(\w+)/", $text, $matches);
// Print a human-readable dump of `$matches[1]`
print_r($matches[1]);
?>

Pattern से String Split करना

preg_replace() किसी pattern का हर match ढूँढता है और इसे replacement text से swap कर देता है, backreferences support करते हुए ताकि आप replacement के अंदर captured groups reuse कर सकें।

यह इसे data reformat करने के लिए उपयोगी बनाता है, जैसे 'lastname, firstname' को एक call में 'firstname lastname' में बदलना।

उदाहरण: Splitting String by Pattern

php
<?php
// Declare `$name`, set to "Doe, Jane"
$name = "Doe, Jane";
// Declare `$result`, set to `preg_replace("/(\w+), (\w+)/", "$2 $1", $name)`
$result = preg_replace("/(\w+), (\w+)/", "$2 $1", $name);
// Print `$result` to the output
echo $result;
?>

Pattern Validation

Delimiters (आमतौर पर forward slashes) एक pattern को wrap करते हैं ताकि PHP को पता चले कि यह कहाँ शुरू और खत्म होता है, और closing delimiter के बाद रखे गए modifier flags matching behavior बदलते हैं, जैसे case-insensitive matching के लिए i या हर line को ^ और $ से अलग treat करने के लिए m।

उदाहरण: Pattern Validation

php
<?php
// Declare `$text`, set to "Hello World"
$text = "Hello World";
// Print a detailed dump (with types) of `preg_match("/hello/i", $text)`
var_dump(preg_match("/hello/i", $text));
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. delimiters भूल जाना, जैसे preg_match(abc, $s), जो एक warning produce करता है क्योंकि किसी pattern को /.../ चाहिए।
  2. यह भूल जाना कि preg_match() matches का एक array नहीं, 1, 0 या false return करता है, इसलिए उन्हें पाने के लिए तीसरा argument इस्तेमाल करें।
  3. pattern में . जैसे special characters escape न करना, ताकि /a.b/ axb से भी match करे।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.