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:
On some systems,
egrep is a deprecated symlink to grep -E. The two commands are functionally equivalent.Quantifiers at a Glance
| Quantifier | Description |
|---|---|
x+ | One or more of x |
x? | Zero or one of x |
x{n} | Exactly n occurrences of x |
x{n,} | At least n occurrences of x |
x{,m} | Up to m occurrences of x |
x{n,m} | Between n and m occurrences |
{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:
? 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.](https://kodekloud.com/kk-media/image/upload/v1752881398/notes-assets/images/Linux-Professional-Institute-LPIC-1-Exam-101-Perform-Basic-File-Management-Part-2-Regular-expressions-2/dark-terminal-sets-examples-kodekloud.jpg)
cat or cut:
Matching Device Files under /dev
A naive '/dev/.*' catches entire paths. Refine step by step:
- Lowercase names + optional digit
- Add uppercase letters
- Allow multiple segments by grouping
![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.](https://kodekloud.com/kk-media/image/upload/v1752881399/notes-assets/images/Linux-Professional-Institute-LPIC-1-Exam-101-Perform-Basic-File-Management-Part-2-Regular-expressions-2/negated-ranges-sets-command-line.jpg)
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.