Chapter 2: Character Classes & Escapes
Goal: Match one character from a specific set, or anything except a set.
1) What is a Character Class?
A character class lets you say: “match any one character from this group”.
Think of it like a multiple-choice question — only one answer is picked.
Pattern: /[aeiou]/
Text: "hello"
Matches: "e", "o" (two separate matches — `preg_match_all()` finds both;
`preg_match()` stops after the first one, "e")
The square brackets [...] define the set. The pattern matches exactly one character that is inside that set.
2) Matching From a Set
List specific characters
Put characters between [ and ]:
preg_match('/[aeiou]/', 'hello', $matches); // matches 'e'
echo $matches[0], PHP_EOL;
preg_match('/[bcd]/', 'cat', $matches); // matches 'c'
echo $matches[0], PHP_EOL;
Use ranges
To avoid listing every letter or digit, use - to define a range:
preg_match('/[0-9]/', 'Age: 42', $matches); // matches '4'
echo $matches[0], PHP_EOL;
preg_match('/[a-z]/', 'Hello', $matches); // matches 'e' (lowercase only)
echo $matches[0], PHP_EOL;
preg_match('/[A-Z]/', 'Hello', $matches); // matches 'H'
echo $matches[0], PHP_EOL;
Ranges work based on character codes. For ASCII letters and digits this is fine. Avoid tricks like [A-z] — we’ll see why later.
3) Negated Character Classes
Prefix with ^ inside the brackets to match anything except what’s in the set:
preg_match('/[^0-9]/', 'ABC123', $matches); // matches 'A' (stops at the first hit)
echo $matches[0], PHP_EOL;
preg_match_all('/[^aeiou]/', 'apple', $matches); // matches 'p', 'p', 'l' — every non-vowel
print_r($matches[0]);
Here, [^0-9] means: “any single character that is not a digit”.
4) Escaping Inside Character Classes
Rules change a little inside [...]. Most characters lose their special meaning there.
- A
.inside a class is a literal dot. No need to escape it. ]is special — escape it with\if you want to match a literal].\is still the escape character.-creates a range by default. Put it at the start or end of the class to match it literally.^only negates when it’s the first character. Elsewhere it’s literal.
preg_match('/[.]/', 'a.b', $matches); // matches '.'
echo $matches[0], PHP_EOL;
preg_match('/[abc-]/', 'a-b', $matches); // matches 'a' or '-' (dash at end)
echo $matches[0], PHP_EOL;
preg_match('/[-abc]/', '-abc', $matches); // matches '-' (dash at start)
echo $matches[0], PHP_EOL;
preg_match('/[\]]/', 'a]b', $matches); // matches ']'
echo $matches[0], PHP_EOL;
Compare with outside a class: /\./ matches a literal dot too, but inside [.] it’s already literal.
5) Shorthand Character Classes
Typing [0-9], [a-zA-Z0-9_] etc. gets repetitive. PCRE provides shorthands:
| Shorthand | Equivalent | Meaning |
|---|---|---|
\d |
[0-9] |
Any digit |
\D |
[^0-9] |
Any non-digit |
\w |
[a-zA-Z0-9_] |
Any “word” character (letters, digits, underscore) |
\W |
[^a-zA-Z0-9_] |
Any non-word character |
\s |
[ \t\r\n\f\v] |
Any whitespace (space, tab, newline, etc.) |
\S |
[^ \t\r\n\f\v] |
Any non-whitespace |
. |
(special) | Any single character except newline (by default) |
preg_match('/\d/', 'Order 123', $matches); // matches '1'
echo $matches[0], PHP_EOL;
preg_match('/\w+/', 'hello_world', $matches); // matches 'hello_world'
echo $matches[0], PHP_EOL;
preg_match('/\s/', "hello world", $matches); // matches space
echo $matches[0], PHP_EOL;
preg_match('/./', 'abc', $matches); // matches 'a'
echo $matches[0], PHP_EOL;
Note: . is not a “character class” in the [...] sense, but it’s related — it matches broadly. We’ll keep using it as we go.
The + in \w+ means “one or more of the preceding” — a quantifier, which chapter 4 covers properly.
6) Unicode Character Properties (a bit more)
If you need to match letters from other languages, digits from other scripts, or emoji, you can use Unicode properties with the u flag.
preg_match('/^\p{L}+$/u', 'café', $matches); // matches (letters)
echo $matches[0], PHP_EOL;
preg_match('/^\p{N}+$/u', '١٢٣', $matches); // matches (Arabic-Indic digits)
echo $matches[0], PHP_EOL;
The ^ and $ around the patterns are anchors: read /^\p{L}+$/u as “only letters from start to end”. They get their own chapter next, with the one exception: $ also accepts a final newline, which \z does not.
The \p{Emoji} property needs a newer engine (PCRE2 10.40, so PHP 8.2 and newer):
preg_match('/\p{Emoji}/u', 'Hi 👋', $matches); // matches emoji — PHP 8.2+
Whether a property is accepted depends on the PCRE2 version bundled with your PHP build: \p{Emoji} needs PCRE2 10.40 or newer (PHP 8.2 and newer bundle it), and an older PHP refuses the pattern outright with a compilation error instead of matching.
Common ones:
| Property | Meaning |
|---|---|
\p{L} |
Any letter (from any language) |
\p{N} |
Any number |
\p{P} |
Punctuation |
\p{S} |
Symbols |
\p{Z} |
Separators (spaces) |
\p{C} |
Other/control characters |
Important: For \p{...} and \P{...} you almost always need the u flag.
If you don’t need Unicode right now, it’s perfectly fine to skip this section and come back to it later.
7) Putting It Together
Character classes shine when combined with other concepts you’ll learn soon:
- Anchors (
^,$) to match the whole string:/^[0-9]+$/means “only digits” - Quantifiers (
+,*,{n,m}) to say “one or more”, “zero or more”, etc.
For example, to check if something is all digits: /^[0-9]+$/ — but don’t worry, we’ll explain ^, $ and + properly in the next chapters.
8) Exercises
Only use what you’ve learned in this chapter and chapter 1.
Exercise 1: Vowels
Write a pattern that matches any vowel (a, e, i, o, u) in lowercase.
Solution
'/[aeiou]/'
Exercise 2: Digits with shorthand
Write a pattern using \d that matches a single digit.
Solution
'/\d/'
Exercise 3: Not a letter
Write a pattern that matches any character that is not a letter (A-Z, a-z).
Solution
'/[^A-Za-z]/'
Exercise 4: Literal dot in a class
Write a pattern using a character class to match a literal dot.
Solution
'/[.]/'
Exercise 5: Word characters
Write a pattern that matches one or more word characters (\w+). Test it mentally on "hello_123".
Solution
'/\w+/'
Key Takeaways
- Character classes
[...]match one character from a set. - Ranges like
[0-9],[a-z]cover consecutive characters. - Negation
[^...]matches one character not in the set. - Shorthands
\d,\D,\w,\W,\s,\Scover common cases. - Inside
[...],.is literal. Watch out for],\,-and^. - For Unicode properties use
\p{...}with theuflag. [A-z]is a common trap — be explicit with[A-Za-z]if you only want letters.
Common Pitfalls
[A-z]is wrong for “all letters” — it includes characters betweenZandain ASCII ([,\,],^,_,`).- Forgetting
ufor\p{}— withoutu, Unicode properties often won’t behave as expected. - Confusing
.and[.]— outside classes.is “any character”, inside[.]it’s a literal dot.
Optional: Tools to Inspect Patterns
If you’d like to visualize or explain a character class, you can use these optional tools. Not required to learn the concepts.
vendor/bin/regex explain '/[aeiou]/'
vendor/bin/regex diagram '/\d+/'
use PHPRegex\Toolkit\Regex;
$regex = Regex::create();
echo $regex->explain('/[^0-9]/');