Complete the code to match any digit character using a regular expression.
<?php $pattern = '/[1]/'; $string = 'My number is 1234'; if (preg_match($pattern, $string)) { echo 'Digit found'; } else { echo 'No digit found'; } ?>
The \d character class matches any digit (0-9).
Complete the code to match exactly three lowercase letters in a row.
<?php $pattern = '/[a-z][1]/'; $string = 'abc def'; if (preg_match($pattern, $string)) { echo 'Match found'; } else { echo 'No match'; } ?>
The quantifier {3} matches exactly three occurrences of the preceding character class.
Fix the error in the pattern to match one or more whitespace characters.
<?php $pattern = '/\s[1]/'; $string = "Hello World"; if (preg_match($pattern, $string)) { echo 'Whitespace found'; } else { echo 'No whitespace'; } ?>
The quantifier + means one or more occurrences, perfect for matching one or more whitespace characters.
Fill both blanks to create a pattern that matches a string starting with one or more digits followed by exactly two letters.
<?php $pattern = '/^[1][a-z][2]$/'; $string = '123ab'; if (preg_match($pattern, $string)) { echo 'Pattern matches'; } else { echo 'No match'; } ?>
The pattern \d+ matches one or more digits at the start, and {2} after a character class means exactly two occurrences.
Fill all three blanks to create a pattern that matches a string with exactly three uppercase letters, followed by one or more digits, and ending with zero or more whitespace characters.
<?php $pattern = '/^[1][2][3]$/'; $string = 'ABC123 '; if (preg_match($pattern, $string)) { echo 'Full match'; } else { echo 'No full match'; } ?>
The pattern matches exactly three uppercase letters [A-Z]{3}, then one or more digits \d+, and ends with zero or more whitespace characters \s*.