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.

  • ๐Ÿ”Ž What they are: Regular expressions, shortened to regexp or regex, describe text patterns for searching and matching.
  • ๐Ÿงฉ Basic set: Symbols such as . ^ $ * and \ anchor, repeat, and escape characters in a pattern.
  • ๐Ÿ”ข Interval: Braces such as {n} and {n,m} control how many times the preceding character repeats.
  • โž• Extended: With grep -E, \+ and \? match one-or-more and zero-or-one occurrences of a character.
  • ๐Ÿงต Brace expansion: Curly braces generate multiple strings from a sequence or a comma-separated list.
  • ๐Ÿ› ๏ธ Where used: Tools including grep, sed, tr, and vi rely on regular expressions to filter text.
  • ๐Ÿค– AI help: AI assistants and GitHub Copilot can generate, explain, and debug regex patterns quickly.

Linux regular expressions with grep โ€” basic, interval, and extended regex examples

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.

cat sample command displaying the contents of an existing file in the terminal

Next, search for content containing the letter ‘a’. The screenshot below shows grep returning every line that includes an ‘a’.

grep searching the sample file for lines that contain the letter a

The caret ‘^’ matches the start of a string. The example below searches for content that starts with the letter ‘a’.

grep with a caret anchor matching only lines that start 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.

Sample file contents shown before filtering lines that end with the letter t

Select only those lines that end with the letter ‘t’ by using the ‘$’ anchor, as shown below.

grep with a dollar anchor selecting only lines that end with the letter t

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.

grep filtering the sample file for lines that contain the character p

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.

grep -E interval expression matching the character p appearing exactly twice

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.

grep searching the sample file for all lines that contain the character t

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.

grep extended expression matching one or more a characters followed by t

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.

echo command using brace expansion to generate multiple strings from a sequence and a list

In the examples above, the echo command creates strings using brace expansion, producing multiple strings out of a single pattern.

FAQs

grep uses basic regular expressions by default. egrep enables extended expressions (the same as grep -E), and fgrep matches fixed strings with no regex (grep -F). Modern GNU grep marks egrep and fgrep as deprecated, so prefer grep -E and grep -F.

Character classes match a category of characters. Bracket expressions like [a-z] match a range, while POSIX classes such as [[:alpha:]], [[:digit:]], and [[:space:]] match letters, numbers, and whitespace. They keep patterns readable and portable across different locales.

Add the -i option, for example grep -i “hello” sample. This matches Hello, HELLO, and hello alike. Without -i, regular expressions are case-sensitive, so an uppercase letter and its lowercase form are treated as two different characters.

Shell wildcards (globbing) expand filenames, where * means any characters and ? means one character. Regular expressions match text inside files, where * means zero or more of the preceding character. The same symbols mean different things in each context.

Use the -o option, for example grep -o “a\+t” sample. Instead of printing the entire line, grep prints only the part that matches the pattern, one match per line. It is useful for extracting values such as emails or numbers.

Use alternation with the pipe symbol. In extended mode, grep -E “cat|dog” sample matches lines containing cat or dog. In basic mode you must escape it as grep “cat\|dog” sample. Alternation lets one command search for several patterns at once.

AI assistants can turn a plain-English description into a working regex, explain what a cryptic pattern does, and suggest fixes when a match fails. Machine-learning tools also detect recurring patterns in large log files that would be tedious to write by hand.

Yes. GitHub Copilot suggests grep commands and regular expressions directly from a comment or a partial pattern as you type. It speeds up writing complex expressions, though you should test any generated pattern against real sample data before relying on it.

Summarize this post with: