Skip to main content
In this lesson, we’ll master extended regular expressions (ERE) using Linux tools like grep -E and egrep. You’ll learn how to apply quantifiers, character classes, alternation, and grouping to perform powerful text searches.

Enabling Extended Regex: -E vs egrep

By default, grep treats metacharacters such as +, ?, {}, |, and () literally. To activate these operators without escaping them, add the -E option or use egrep:
Or simply:
On some systems, egrep is a deprecated symlink to grep -E. The two commands are functionally equivalent.

Quantifiers at a Glance

{n,} – At Least n Matches

Find lines with three or more consecutive zeros:

{,m} – At Most m Matches

Match up to three zeros (including none):

{n} – Exactly n Matches

Search for exactly three zeros:

? – Optional Element

Make the preceding character optional (0 or 1):

{n,m} – Range of Matches

Match between 3 and 5 zeros:

Alternation with |

Use | to choose between patterns. To match enabled or disabled:
Combine ? to make the final letter optional:

Character Sets and Ranges

Define a set of acceptable characters with square brackets:
  • [abc123] matches one of a, b, c, 1, 2, or 3
  • [a-z] matches one lowercase letter
  • [0-9] matches one digit
The image shows a dark-themed terminal interface with a focus on ranges or sets, displaying examples like [a-z] and [0-9]. The word "KodeKloud" is visible in the top right corner.
Example: match cat or cut:

Matching Device Files under /dev

A naive '/dev/.*' catches entire paths. Refine step by step:
  1. Lowercase names + optional digit
  2. Add uppercase letters
  3. Allow multiple segments by grouping
/etc/sane.d/dc210.conf:port=/dev/tty0P0 …
The image shows a dark-themed interface with a command line prompt on the left and a section on the right displaying character sets like "[abc123]" and "[a-z]". The title "Negated Ranges Or Sets" is at the top.
Example: find slashes not followed by lowercase:

Beyond grep

Extended regex patterns also work with sed, awk, perl, and many text editors. A great hands-on resource is regexr.com for interactive testing and learning.

Watch Video