Linux Regular Expression Tutorial: Grep Regex Example
โก Smart Summary
Linux regular expressions are special character sequences that search data and match complex patterns across tools such as grep, sed, and vi, using basic, interval, extended, and brace-expansion forms to filter text precisely.

Regular expressions are one of the most powerful features of Linux text tools, letting you match, filter, and transform data with compact patterns. This page explains the basic, interval, and extended forms, plus brace expansion, with worked grep examples.
What are Linux Regular Expressions?
Linux regular expressions are special characters that help search data and match complex patterns. Regular expressions are shortened as ‘regexp’ or ‘regex’. They are used in many Linux programs such as grep, bash, rename, sed, and more.
Types of Regular expressions
For ease of understanding, let us learn the different types of regular expressions one by one. This page covers basic, interval, and extended regular expressions, and finishes with brace expansion.
Basic Regular expressions
Some of the commonly used commands that work with regular expressions are tr, sed, vi, and grep. The table below lists some of the basic regular expressions.
| Symbol | Description |
|---|---|
| . | Replaces any character |
| ^ | Matches the start of a string |
| $ | Matches the end of a string |
| * | Matches zero or more times of the preceding character |
| \ | Represents special characters |
| () | Groups regular expressions |
| ? | Matches exactly one character |
Let us see an example. First, run cat sample to display the contents of an existing file, as shown below.
Next, search for content containing the letter ‘a’. The screenshot below shows grep returning every line that includes an ‘a’.
The caret ‘^’ matches the start of a string. The example below searches for content that starts with the letter ‘a’.
Only lines that start with the character are filtered. Lines that do not contain the character ‘a’ at the start are ignored.
Let us look at another example. The screenshot below again shows the sample file contents before filtering by the end of a line.
Select only those lines that end with the letter ‘t’ by using the ‘$’ anchor, as shown below.
Interval Regular expressions
These expressions specify the number of occurrences of a character in a string. They are listed below.
| Expression | Description |
|---|---|
| {n} | Matches the preceding character appearing ‘n’ times exactly |
| {n,m} | Matches the preceding character appearing ‘n’ times but not more than m |
| {n, } | Matches the preceding character only when it appears ‘n’ times or more |
For example, filter out all lines that contain the character ‘p’. The screenshot below shows the matching lines.
Now check that the character ‘p’ appears exactly two times in a string, one after the other. The syntax for this is:
cat sample | grep -E p\{2}
The result of the interval match is shown below.
Note: You need to add -E with these regular expressions.
Extended regular expressions
These regular expressions combine more than one expression. Some of them are:
| Expression | Description |
|---|---|
| \+ | Matches one or more occurrence of the previous character |
| \? | Matches zero or one occurrence of the previous character |
For example, search for all occurrences of the character ‘t’. The screenshot below shows the result.
Suppose you want to filter out lines where the character ‘a’ precedes the character ‘t’. You can use a command like the one below:
cat sample|grep "a\+t"
The output is shown in the following screenshot.
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 ‘..’.
Some examples are shown below.
In the examples above, the echo command creates strings using brace expansion, producing multiple strings out of a single pattern.










