Welcome back to CodeYourCraft! Today, we're diving into the fascinating world of PHP Regular Expressions (Regex) and focusing on Quantifiers. These little helpers will enable you to repeat patterns in your Regex, making your code more efficient and powerful. Let's get started!
In simple terms, Quantifiers control how many times a specific character, group, or pattern should be matched. They help us to find exact matches according to the defined criteria.
There are four basic quantifiers in PHP Regex: ?, *, +, and {n}. Let's understand them one by one.
? - Zero or One OccurrenceThe ? quantifier matches the preceding character zero or one time.
π Example: To find the word 'dog' or 'do' in a string, we can use the following Regex: /dog?/.
$text = "I have a dog and two cats.";
// Search for 'dog' or 'do' in the text
$pattern = '/dog?/';
preg_match_all($pattern, $text, $matches);
print_r($matches);Output:
Array
(
[0] => Array
(
[0] => dog
)
)
* - Zero or More OccurrencesThe * quantifier matches the preceding character zero or more times.
π Example: To find all occurrences of the word 'car' in a sentence, even if it's repeated multiple times, we can use the following Regex: /car*/.
$text = "I have a car, I love cars, and I need another car.";
// Search for 'car' in the text
$pattern = '/car*/';
preg_match_all($pattern, $text, $matches);
print_r($matches);Output:
Array
(
[0] => Array
(
[0] => car
[1] => car
[2] => car
)
)
+ - One or More OccurrencesThe + quantifier matches the preceding character one or more times.
π Example: To find all words starting with 'a' and having one or more letters following it in a sentence, we can use the following Regex: /a+/.
$text = "Apple, apricot, avocado, and many more fruits.";
// Search for words starting with 'a' and having one or more letters following it
$pattern = '/a+/';
preg_match_all($pattern, $text, $matches);
print_r($matches);Output:
Array
(
[0] => Array
(
[0] => Apple
[1] => apricot
[2] => avocado
)
)
{n} - Exact Number of OccurrencesThe {n} quantifier matches the preceding character exactly n times.
π Example: To find exactly three occurrences of the letter 'o' in a string, we can use the following Regex: /ooo/.
$text = "This is a three-letter word with three 'o's.";
// Search for exactly three occurrences of 'o' in the text
$pattern = '/ooo/';
preg_match($pattern, $text, $matches);
print_r($matches);Output:
Array
(
[0] => three-letter word with three 'o's.
)
Which quantifier matches the preceding character zero or more times in PHP Regex?
By default, quantifiers in PHP Regex are greedy, meaning they match as much as possible. However, sometimes we may want to match the minimum occurrence instead. For this, we can use lazy quantifiers by adding a question mark (?) after the quantifier.
π Example: Let's find all the email addresses in a text. A typical email address contains an @ symbol, followed by one or more characters, followed by a dot, and then one or more characters.
With a greedy .* quantifier, the regex might match too much and capture unwanted characters. To fix this, we can make the dot lazy by adding a question mark (.?).
$text = "My email addresses are example@example.com, info@example.co.uk, and support@example.net.";
// Search for email addresses
$pattern = '/b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}/';
preg_match_all($pattern, $text, $matches);
print_r($matches);Output:
Array
(
[0] => Array
(
[0] => example@example.com
[1] => info@example.co.uk
[2] => support@example.net
)
)
Which quantifier matches the preceding character one or more times, but as little as possible in PHP Regex?
Now that you've learned about PHP Regex Quantifiers, try to solve the following problems. Answers can be found in the comments below.
$text = "My phone numbers are (123) 456-7890, +44 20 7890 1234, and 0987654321.";
// Find phone numbers
// Hint: Use a combination of character classes ([]), quantifiers, and grouping (())$text = "#CodeYourCraft #LearnPHP #BeginnerFriendly #Tutorial";
// Find hashtags
// Hint: Use character classes ([]) and quantifiersThat's it for today! Keep practicing and don't forget to check back for more PHP tutorials on CodeYourCraft. Happy coding! π»π