🔴 Advanced  ·  Lesson 30

Regular Expressions

Regular Expressions

What is Regex?

Regular expressions (regex) are patterns to search, match and replace text. PHP uses preg_match() and preg_replace().

preg_match (does it match?)

$email = "test@site.com";
if (preg_match("/^[\w.]+@[\w.]+\.\w+$/", $email)) {
    echo "Valid email";
}  // Valid email

preg_replace (find and replace)

$text = "Call me at 99999-88888";
$clean = preg_replace("/[0-9]/", "*", $text);
echo $clean;  // Call me at *****-*****

Common Patterns

PatternMatches
\da digit
\wletter/digit/_
+one or more
*zero or more

Summary

  • Regex matches/replaces text patterns; use preg_match and preg_replace.
  • \d = digit, \w = word char, + = one or more.

Regex क्या है?

Regular expressions (regex) text को search, match और replace करने के patterns हैं। PHP preg_match() और preg_replace() use करता है।

preg_match (match होता है क्या?)

$email = "test@site.com";
if (preg_match("/^[\w.]+@[\w.]+\.\w+$/", $email)) {
    echo "Valid email";
}  // Valid email

preg_replace (find और replace)

$text = "Call me at 99999-88888";
$clean = preg_replace("/[0-9]/", "*", $text);
echo $clean;  // Call me at *****-*****

Common Patterns

PatternMatches
\ddigit
\wletter/digit/_
+एक या ज़्यादा
*zero या ज़्यादा

सारांश

  • Regex text patterns match/replace करता है; preg_match और preg_replace use करें।
  • \d = digit, \w = word char, + = एक या ज़्यादा।
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।