Regular Expressions Cheatsheet

Master the bread and butter of Linux scripting with this Regular Expressions cheatsheet.

Regular Expressions Cheatsheet 00 Featured Image

If you work with text, you’ll appreciate how useful regular expressions are. These are small characters of text which allow you to create elaborate rules on what a word looks like. These rules can either be as simple as matching a single letter in a document or something complex such as looking for every word that begins in “a” and “c” but ends in “ism.”

Regular Expressions Cheatsheet Download

Download this Cheatsheet

Enter your email below to receive this PDF cheatsheet in your Inbox.

Essential Regular Expressions

Regular expressions (or regex for short) are everywhere in Linux environments for searching through text right down to the character. This Regular Expressions cheatsheet will be useful for people who simply need a little refresher from time to time.

One important thing to note, however, is that the set of usable regular expressions largely depend on the type of standard that a software uses. For example, common Linux terminals often use the POSIX standard while Vim and Perl uses non-standard definitions.

MetacharacterDescriptionExample
Character Matching
ALook for any instance of the letter “a” in a document and match it.a matches “ant”, “cat” and “anna”.
.Match any single character, except control characters.c.t matches “cat”, “cut” or “cot”.
*Repeat the previous expression 0 or more times (greedy mode).12*3 matches “13”, “123”, “1223”, “12223”. It can be use together with “.” such as m.*easier matches “maketecheasier”. Using .* by itself is meaningless as it matches everything and return the full result.
+Repeat the previous expression 1 or more times.12+3 matches “123”,”1223″,”12223″.
?Makes the previous item optional.ma?ke matches “make”, “mke”.
^Match from the beginning of the string.^he matches “hello”, “hell”, “help”, “he is a boy”.
$Match from the end of the string.ed$ matches “acted”, “bed”, “greed”.
Range Matching
( … )Grouping of characters or expression.(ak) matches “make”, “take”.
{ n }Match the previous item exactly n times.12{3}5 matches “12225”.
[ … ]Match a single character in the bracket.[abc] matches “a”,”b” or “c” in the string “abc”.
[^ … ]Match any character except for those that are defined in the bracket.a[^b]c matches “aec”, “acc”, “adc”, but not “abc”.
Class Matching (POSIX)
[:alpha:]Look for any character in the English alphabet and match it.[[:alpha:]]ut will match the words “but”, “cut” and “rut”.
[:alnum:]Look for any character and number and match it.[[:alnum]]tack will match “stack” and “5tack”.
[:blank:]Look for an instance of either a tab or a space and match it.[[:blank:]]Hello will match ” Hello”.
[:space:]Search for any whitespace character and match it.[[:space:]]World will match ” World”.
[:upper:]Look for any uppercase letter and match it.[[:upper:]]+ will match the words “HELLO”, “FAntastic” and “wORld”.
[:lower:]Look for any lowercase letter and match it.[[:lower:]]+ will match the words “heLLO”, “FAntastic” and “world”.
[:punct:]Search for any punctuation character and match it.\w+[[:punct:]] will match the following words: “Hey!”, “Hi.” and “You?”.
[:cntrl:]Look for any control character and match it.\w+[[:cntrl:]]$ will match the following words: “end^M”, “word^M” and “this^M”.
[.ng.]Look for any digraph of “ng” and match it.bad[.zh.] will match the word “badge”.
[=n=]Find and match all the diacritical letters of “n”.[=a=]\w+ will match the words “ànna”, “ápple” and “âble”.
Class Matching (Perl/Vim)
\BLook for any character that is not a word boundary and match it.\B+\b will match “o ” in “hello ” as well as ” h” in ” hi”.
\wLook for any letter and number, then match it.\w{5} will match the following combination of letters and numbers: “world”, “12345” and “w0r1d”.
\WSearch for any symbols and match it.\w{2}\W\b will match the following words: “as.”, “ha!” and “me?”.
\dLook for any number and match it.\d{3} will match the following numbers: “111”, “777” and “888”.
\DLook for any character that is not a number and match it.\D{3} will match the following letters and symbols: “ant”, “ba!” and “!#*”.
\sLook for any whitespace characters and match it.a\w+(i|y)sm\s will match the following words: “altruism “, “albinism ” and “aneurysm “.
\SSearch for any non-whitespace characters and match it.\S{5} will match the following: “123hi”, “hi!^&” and “
\AFind the beginning of a word regardless of it not being at the start of a line.\A{2} will match “He” and “wo” in “Hello world”.
\ZFind the end of a word regardless of it not being at the end of a line.\z{2} will match “lo” and “ld” in “Hello world”.
Control Characters
|Match either the expression on the left or right of the pipe.col(o|ou)r matches “color”, “colour”.
Specify a range of characters to match. Used mostly in [a-z], [A-Z],[1-9],[a-zA-Z1-9].a[a-z]c matches “abc”, “acc”, “adc”.
\Escape a special character and turn it into an ordinary character.a\*c matches “a*c”.
\n, \r, \tMatch a newline, return and tab character respectively.\w{5}\t\w{5}(\n|\r) will match “Hello World”.
\b…\bMatch a word within the boundary.\bTech\b matches the word “Tech” in “Make Tech Easier”.
Complex Expressions
[0-9]{3}-[0-9]{4}Look for any sequence of three and four digit numbers and match it.This expression will match the following combination of numbers: “123-1234”, “111-1111” and “777-1234”.
([0-9]{3}-)?[0-9]{3}-[0-9]{4}Match two three digit numbers and a single four digit number.This expression will match the following combinations: “123-123-1234”, “000-111-0001” and “777-5678”.
.{1,3}Look for any word that is between one and three characters long.This expression will match the following words: “at”, “it” and “can”.
([A-Z])\w+Match any word that begins with a capital letter.This expression will match the following words: “Hello”, “World” and “Hey”.
e[([:digit:]|[:alnum:])]cMatch any alphanumeric character between “e” and “c”.This expression will match the following combinations: “e1c”, “e5c” and “e7c”.
[^0-9[:upper:]]+Look for any fully capitalized word that does not begin with a number.This expression will match the following words: “HELLO”, “WORLD” and “HEY”.
“.+”Find a quoted word and match it.This expression will match the following quote: “Hello, world!”.

Image credit: Unsplash

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox