H
Hype Drip

How to use regular expressions (regexes) on linux

Author

Daniel Moore

Published Mar 29, 2026

What are Linux Regular Expressions?

Linux Regular Expressions are special characters which help search data and matching complex patterns. Regular expressions are shortened as ‘regexp’ or ‘regex’. They are used in many Linux programs like grep, bash, rename, sed, etc.

Types of Regular expressions

For ease of understanding let us learn the different types of Regex one by one.

  • Basic Regular expressions
  • Interval Regular expressions
  • Extended regular expressions
  • Summary

Click here if the video is not accessible

Basic Regular expressions

Some of the commonly used commands with Regular expressions are tr, sed, vi and grep. Listed below are some of the basic Regex.

SymbolDescriptions
.replaces any character
^matches start of string
$matches end of string
*matches up zero or more times the preceding character
\Represent special characters
()Groups regular expressions
?Matches up exactly one character

Let’s see an example.

Execute cat sample to see contents of an existing file

How to use regular expressions (regexes) on linux

Search for content containing letter ‘a’.

How to use regular expressions (regexes) on linux

^‘ matches the start of a string. Let’s search for content that STARTS with a

Only lines that start with character are filtered. Lines which do not contain the character ‘a’ at the start are ignored.

Let’s look into another example –

How to use regular expressions (regexes) on linux

Select only those lines that end with t using $

How to use regular expressions (regexes) on linux

Interval Regular expressions

Filter out all lines that contain character ‘p’

We want to check that the character ‘p’ appears exactly 2 times in a string one after the other. For this the syntax would be:

Note: You need to add -E with these regular expressions.

Extended regular expressions

Searching for all characters ‘t’

How to use regular expressions (regexes) on linux

Suppose we want to filter out lines where character ‘a’ precedes character ‘t’

We can use command like

Brace expansion

The syntax for brace expansion is either a sequence or a comma separated list of items inside curly braces ““. The starting and ending items in a sequence are separated by two periods “..”.

How to use regular expressions (regexes) on linux

In the above examples, the echo command creates strings using the brace expansion.