Regex Cheat Sheet for Beginners

Regular expressions look cryptic until you learn the handful of building blocks. Here's a beginner's cheat sheet.

Character classes

PatternMatches
.any character (except newline)
\da digit (0–9)
\wword character (a–z, A–Z, 0–9, _)
\swhitespace
[abc]a, b, or c
[^abc]anything except a, b, c
[a-z]any lowercase letter

Quantifiers

PatternMeaning
*0 or more
+1 or more
?0 or 1 (optional)
{3}exactly 3
{2,5}between 2 and 5

Anchors

PatternMeaning
^start of string/line
$end of string/line
\bword boundary

Groups and alternation

  • (abc) — a capturing group
  • (?:abc) — a non-capturing group
  • a|b — match a or b

Common flags

  • g — global (find all matches)
  • i — case-insensitive
  • m — multiline (^/$ match each line)

A worked example

Match a simple time like 09:30:

^\d{2}:\d{2}$

Two digits, a colon, two digits, anchored to the whole string.

Practice

Test patterns live against your own text with the regex tester, and see common regex patterns for ready-made examples.

Got a config file to check?

Open the config toolkit →