Regular expressions also known as Regex. These are a powerful tool used for pattern matching and text manipulation. It provide a flexible way to find, replace, and validate text or strings.
It used to match given or specific patterns within a string.
$string = "The sample text paragraph with some few words in the line.";
$pattern = "/sample text/";
if (preg_match($pattern, $string)) {
echo "Match found!";
}
$pattern = "/\d{3}-\d{4}-\d{2}/";
$string = "Phone number: 123-4567-89.";
if (preg_match($pattern, $string, $matches)) {
echo "Extracted Phone number: " . $matches[0];
} else {
echo "No match found";
}
To find and replace the given text using regular expression.
$string = "The sample text paragraph with some few words in the line.";
$newString = preg_replace("/few/", "more", $string);
echo $newString; // Output: The sample text paragraph with some more words in the line.
- To extract specific part of the text from given string.
$string = "Kumara Vel (kumaravel@host.com)";
preg_match("/\((.*)\)/", $string, $matches);
echo $matches[1]; // Output: kumaravel@host.com
- To match and extract all text from given string.
$string = "The sample text paragraph with sample few words in the line.";
preg_match_all("/sample/", $string, $matches);
print_r($matches);
- To split text with given pattern.
$string = "apple, banana, orange";
$pattern = "/, /";
$fruits = preg_split($pattern, $string);
print_r($fruits);
To check or validate text in given format.
$email = "kumaravel@host.com";
if (preg_match("/^\w+@\w+\.\w+$/", $email)) {
echo "Valid email address";
} else {
echo "Invalid email address";
}
- Character Classes:
[abc]
: Matches any single character from the set.[^abc]
: Matches any single character not in the set.[a-z]
: Matches any lowercase letter.[A-Z]
: Matches any uppercase letter.[0-9]
: Matches any digit.
- Quantifiers:
+
: Matches one or more repetitions of the preceding element.*
: Matches zero or more repetitions of the preceding element.?
: Matches zero or one repetition of the preceding element.{n}
: Matches exactly n repetitions.{n,}
: Matches at least n repetitions.{n,m}
: Matches at least n and at most m repetitions.
- Anchors:
^
: Matches the beginning of the string.$
: Matches the end of the string.
- Grouping:
()
: Groups a part of the pattern.{}
: Matches a specific number of occurrences of the preceding element.[]
: Matches a character within the specified set.
- Alternative:
|
: Matches either the expression before or after the symbol.
- Metacharacters:
.
: Matches any single character except a newline.\d
: Matches a digit character.\d{2}
: Matches a two digit character.\d+
: Matches a multiple digit character.\w
: Matches any word character (alphanumeric or underscore).\w{5}
: Matches a five letter word.\w+
: Matches a multiple character word.\W
: Matches any non-word character.\s
: Matches any whitespace character.\S
: Matches any non-whitespace character.