Regular Expressions Cheat Sheet

If you work with text, you’ll appreciate how useful regular expressions are. Regular expressions are everywhere in Linux for searching through text right down to the character. This article aims to provide a small “cheat sheet” for people who simply need a little refresher from time to time.

If you need some introduction to regular expressions, check out our beginner guide here.

. (dot)Match any single character, except newline (\n)c.t matches "cat", "cut" or "cot."
* (star)Repeat the previous expression 0 or more times (greedy mode)12*3 matches "13", "123", "1223", "12223". It can be use together with . (dot) such as m.*easier matches "maketecheasier". Using .* by itself is meaningless as it matches everything and return the full result.
+ (plus)Repeat the previous expression 1 or more times. 12+3 matches "123","1223","12223"
? (question mark)Makes the previous item optional. ma?ke matches "make", "mke"
^ (caret)Match from the beginning of the string^he matches "hello", "hell", "help", "he is a boy"
$ (dollar)Match from the end of the stringed$ matches "acted", bed", "greed"
(...) (round bracket)Grouping of characters or expression(ak) matches "make", "take", '
{n} (curly bracket, where n is an integer bigger than 0)Match the previous item exactly n times12{3}5 matches "12225"
[...] (square bracket)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 bracketa[^b]c matches "aec", "acc", "adc", but not "abc"
| (pipe)Match either the expression on the left or right of the pipe.col(o|ou)r matches "color", "colour"
- (hypen)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"
\ (backslash)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
\b...\bMatch a word within the boundary.\bTech\b matches the word "Tech" in "Make Tech Easier".

Some More Complex Examples

Matching a certain number of characters.

Here’s an example for a U.S. phone number, not counting the area code:

This will match any phone number of the format “111-1111”.

Making a pattern optional

Here’s the U.S. phone number example again, this time with optional area codes. We’ll assume that the file with the phone numbers we’re looking for has phone numbers structured like this: 555-555-5555. The “?” operator denotes an optional pattern preceding it.

Sure, it’s a bit ugly, as regular expressions tend to be, but it’s very powerful.

Finding a range of characters:

This will match any character between 1 and 3 times.

Download Regular Expressions Cheat Sheet

Can’t get enough of this? We have prepared a downloadable cheat sheet for you so you can access to it when you need it.

Download it here.


  1. Hi, nice cheat sheet, thanks for this topic.

    But CMIIAW, it seems to me that
    “[…] (square bracket) match exactly the string or expression in the bracket” is a false explanation.

    Purpose of a bracket expression is: Matches _a_single_character that is contained within the brackets. For example, [abc] matches “a”, “b”, or “c”. [a-z] specifies a range which matches any lowercase letter from “a” to “z”. These forms can be mixed: [abcx-z] matches “a”, “b”, “c”, “x”, “y”, or “z”, as does [a-cx-z].

    This has nothing to do with string or expression, it matches a single character.


    — Philippe

  2. There is a mistaken Regex here, to match this :555-555-5555

    You wrote: ([0-9]{3})?[0-9]{3}-[0-9]{4}

    but the first hyphen is missing. In the first group, it should be placed just before the first closing parenthese:


    Is this right?

Comments are closed.