Chad: “Sometimes Regex feels like somebody spilled punctuation onto a text file and then declared it a language. ๐”
Regular Expressions are a pain in the you-know-where at times. Even the instructors who taught the Google IT Automation Certificate where like “nobody remembers this stuff, just use the free tools available online”.
I couldn’t remember where to find those tools, so I thought i’d post the next best thing here on my website instead.
The Essentials
| Pattern | Meaning | Example |
|---|---|---|
. | Any character except newline | c.t matches cat, cot, cut |
\d | Any digit | \d\d matches 42 |
\D | Not a digit | \D matches A |
\w | Letter, digit, or underscore | \w+ matches hello123 |
\W | Not a word character | @ |
\s | Whitespace (space, tab, newline) | \s+ |
\S | Non-whitespace | hello |
Quantifiers
These answer “how many?”
| Pattern | Meaning |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 |
{3} | Exactly 3 |
{2,5} | Between 2 and 5 |
{2,} | At least 2 |
Examples:
a*
Matches:
"", "a", "aa", "aaa"
This:
\d+
Matches:
1
42
12345
These answer “where?”
Anchors
| Pattern | Meaning |
|---|---|
^ | Start of string |
$ | End of string |
Example:
^\d+$
Matches:
12345
But not:
abc123
123abc
because the whole string must be digits.
Character Sets
Match one character from a group:
[abc]
Matches:
a
b
c
Ranges:
[a-z]
Lowercase letters.
[A-Z]
Uppercase letters.
[0-9]
Digits.
Negation:
[^0-9]
Matches anything except digits.
OR
cat|dog
Matches either:
cat
dog
Grouping
(ab)+
Matches:
ab
abab
ababab
Escaping Special Characters
Need a literal dot?
\.
Need a literal dollar sign?
\$
Need a literal question mark?
\?
Common Patterns
Match a phone number
^\d{3}-\d{3}-\d{4}$
Matches:
555-123-4567
Match an email
Simple version:
^\w+@\w+\.\w+$
Matches:
karl@gmail.com
Match only letters
^[A-Za-z]+$
Matches:
Hello
Not:
Hello123
Match a ZIP code
^\d{5}$
Matches:
33023
The Four I Use Constantly
If you remember only these, you’re already dangerous:
\d
digit
\w
word character
+
one or more
^...$
match the entire string
For example:
^\d+$
is basically:
“The whole string must be one or more digits.”
These are probably the single most useful regex you’ll encounter in beginner coding challenges. ๐


Leave a Reply