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.

PatternMeaningExample
.Any character except newlinec.t matches cat, cot, cut
\dAny digit\d\d matches 42
\DNot a digit\D matches A
\wLetter, digit, or underscore\w+ matches hello123
\WNot a word character@
\sWhitespace (space, tab, newline)\s+
\SNon-whitespacehello

These answer “how many?”

PatternMeaning
*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?”

PatternMeaning
^Start of string
$End of string

Example:

^\d+$

Matches:

12345

But not:

abc123
123abc

because the whole string must be digits.


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.


cat|dog

Matches either:

cat
dog

(ab)+

Matches:

ab
abab
ababab

Need a literal dot?

\.

Need a literal dollar sign?

\$

Need a literal question mark?

\?

^\d{3}-\d{3}-\d{4}$

Matches:

555-123-4567

Simple version:

^\w+@\w+\.\w+$

Matches:

karl@gmail.com

^[A-Za-z]+$

Matches:

Hello

Not:

Hello123

^\d{5}$

Matches:

33023

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. ๐Ÿš€


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *