• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Regex Cheatsheet

Posted Oct 27, 2023 — Updated Jan 11, 2024

Regex is very powerful and available in virtually every language. Eventually I’ll write a whole post about why every engineer should know some Regex, but this post is all about common regex patterns, flags, etc—in easy-to-read table form.

First, here are some patterns similar to ones I’ve used in recent memory:

PatternMeaningExample Match
\d+match 1 or more digitsabc123def
\w+$match 1 or more word chars before end of lineabc-123def
ab|dematch ab or deabc123def
(?<=:)\w{4}match exactly 4 word characters preceded by a “:”abc:123def
(?<=\w+):match a : preceded by 1 or more word charactersabc: 123

Now to break down the common Regex elements.

Regex Character Classes

CharacterMeaning
\wany alphanumeric character, does not match accented characters1
\Wany non-alphanumeric character
\swhitespace (includes tabs, line breaks)
\Snot whitespace
\dany digit (same as doing [0-9])
\Dnot digits
.match any character (except line breaks)
[ab]match any character inside the [] (same as a|b)

Regex Anchors

AnchorMeaning
^the start of a line
$the end of a line
\ba word boundary
\Bnot a word boundary

Regex Quantifiers

QuantifierMeaning
+match 1 or more of the preceding token
*match 0 or more of the preceding token
{1, 2}match 1 or 2 of the preceding token
{2}match exactly 2 of the preceding token
{2,}match 2 or more of the preceding token

Grouping, Lookaheads, and Lookbehinds2

SyntaxMeaningExample Match
(abc)match the full group abcabc123
a|bmatch a or b, | is the regex “or”attribute
[abc]match any of a, b, or cattribute
[^abc]match not a, b, or ccarbs
[a-z]match any character in the range a-zaBc123
(?=abc)abc must come after your match: hi(?=abc)hiabc
(?!abc)abc cannot follow your match: hi(?!abc)hi_there
(?<=abc)abc must precede your match: (?<=abc)hiabchi
(?<!abc)abc cannot precede your match: (?<!abc)hi well_hi

There is a lot more to regex, but combinations of the above classes, anchors, quantifiers, and groupings will get you very far. I recommend Regexr for a quick regex test bed and reference page, I’ve used it countless times (including while writing this post).


Helpful Links

  • An expression to match all characters, including accented ones

Notes

  1. \w catches ascii word characters. Accented characters, like é, are unicode. Accented characters will also be interpreted as word boundaries (\b), so beware! For a starting point on how to handle unicode characters, try the helpful link above. ↩︎
  2. This is one area where you might run into some language-specific limitations. Golang, for instance, doesn’t support lookbehind matches with non-deterministic lengths ↩︎

Filed Under: Productivity, Regex, Tips

Reader Interactions

Trackbacks

  1. Replace a string in Python | Justin Joyce says:
    October 29, 2023 at 3:21 pm

    […] (replace with empty string), I tend to use regex. Regex is a deep topic, and I actually wrote up a regex cheatsheet post, but here’s a simple re.sub […]

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy