Regular Expression (Regex) Cheat Sheet

Regex (short for regular expression), is a string of text that allows you to create patterns that help match, locate, and manage text.

Jump to: Regex Tester Tool

What is Regex?

Regular expressions are notations for describing patterns of text and, in effect, make up a special-purpose language for pattern matching. Although there are myriad variants, all share the idea that most characters in a pattern match literal occurrences of themselves, but some metacharacters have special meaning, such as * to indicate some kind of repetition or […] to mean any one character from the set within the brackets.

Beautiful Code by Andy Oram, Greg Wilson

More simply, Regex (short for regular expression), is a string of text that allows you to create patterns that help match, locate, and manage text.

Below is a quick reference Javascript regex cheat sheet. If you need a more in depth refresher or a place to get started I recommend these resources on regex:

LanguageReference Materials
JavacriptRegular expressions – JavaScript | MDN (mozilla.org)

JavaScript RegExp Reference (w3schools.com)
T-SQL (MS SQL Server)Search Text with Regular Expressions – SQL Server
Management Studio (SSMS) | Microsoft Docs
PythonRegular Expression HOWTO — Python 3.9.6 documentation

Python RegEx (w3schools.com)
C LanguagesRegular expressions in C – GeeksforGeeks

An example of using regular expressions in C (lemoda.net)

Regex Class (System.Text.RegularExpressions) | Microsoft Docs (C#)
Pearl (PCRE)perlre – Perl regular expressions – Perldoc Browser
Powershellabout Regular Expressions – PowerShell | Microsoft Docs
JavaJava Regular Expressions (w3schools.com)

Lesson: Regular Expressions (The Java™ Tutorials > Essential Java Classes) (oracle.com)
Linux (Bash)How to Use Regular Expressions (regexes) on Linux (howtogeek.com)

Using Grep + Regex (Regular Expressions) to Search Text in Linux | DigitalOcean

Advanced Bash regex with examples – Linux Tutorials – Learn Linux Configuration
Other Resources and Reference MaterialComparison of regular-expression engines – Wikipedia

An introduction to regular expressions – O’Reilly (oreilly.com)
Regex Reference Materials

Quick Reference Cheat Sheet

Character classes
.any character except newline
\w\d\sword, digit, whitespace
\W\D\Snot word, digit, whitespace
[abc]any of a, b, or c
[^abc]not a, b, or c
[a-g]character between a & g
Anchors
^abc$start / end of the string
\b\Bword, not-word boundary
Escaped characters
\.\*\\escaped special characters
\t\n\rtab, linefeed, carriage return
Groups & Lookaround
(abc)capture group
\1backreference to group #1
(?:abc)non-capturing group
(?=abc)positive lookahead
(?!abc)negative lookahead
Quantifiers & Alternation
a*a+a?0 or more, 1 or more, 0 or 1
a{5}a{2,}exactly five, two or more
a{1,3}between one & three
a+?a{2,}?match as few as possible
ab|cdmatch ab or cd
Regex Cheat Sheet

Regex Tester Tool

Disclaimer: For educational and demonstrative purposes only. Always test your regular expressions before applying anything to a production system. Results from the above tool are not guaranteed.

How To Use:

  1. Type a regular expression in the Regex input box. (Leading and ending slashes are added automatically)
  2. Type a sample string to match against in the other box.
  3. Check out the resulting matches.
  4. Tweak your regex expression until you get the expected results to test your knowledge.

Avery Lane

Author: Avery Lane

Hi, I'm Avery and I think computers and math are cool.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.