$string = 'The product code is MBT-3461.';
preg_match('/MB[TF]/', $string); // Matches MBT and returns 1
preg_match('/[.]/', $string); // Matches . and returns 1
preg_match('/[13579]/', $string); // Matches 3 and returns 1
| Character | Example | Meaning |
|---|---|---|
| ^ | [^aeiou] | Negates the list of characters inside the character class |
| - | [a-z] | Creates a range of characters based on their Latin-1 character set values |
preg_match('/MB[^TF]/', $string); // Matches nothing and returns 0
preg_match('/MBT[^^]/', $string); // Matches MBT- and returns 1
preg_match('/MBT-[1-5]/', $string); // Matches MBT-3 and returns 1
preg_match('/MBT[_*-]/', $string) // Matches MBT- and returns 1
| Pattern | Matches |
|---|---|
| [:digit:] | Digits (same as \d) |
| [:lower:] | Lowercase letters |
| [:upper:] | Uppercase letters |
| [:letter:] | Upper-and lowercase letters |
| [:alnum:] | Upper-and lowercase letters and digits |
| [:word:] | Upper-and lowercase letters, digits, and the underscores (same as \w) |
| [:print:] | All printable characters including the space |
| [:graph:] | All printable characters excluding the spacet |
| [:punct:] | All printable characters excluding letters and digits |