Extended Regular Expressions (ERE) let you write more expressive patterns without backslash escapes for common operators. In this guide, you’ll learn how to use quantifiers, character classes, grouping, alternation, and negation with GNU grep (and similar tools) to craft powerful searches.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Using grep -E vs egrep
Bothgrep -E and egrep enable ERE syntax, so you don’t need to escape +, ?, {}, or |:
| Command | Description |
|---|---|
grep -Er '0+' /etc/ | Recursively search for one or more 0 in /etc/. |
egrep -r '0+' /etc/ | Same as above using egrep. |
Under the hood,
egrep is equivalent to grep -E. Future versions of GNU grep may deprecate egrep.Curly-Brace Quantifiers
Curly braces let you specify exact or range-based repetition counts:| Syntax | Meaning |
|---|---|
{n} | Exactly n occurrences |
{n,} | At least n occurrences |
{,m} | At most m occurrences |
{n,m} | Between n and m occurrences |
Examples
? and * Quantifiers
x?– zero or onexx*– zero or morex
disable and disabled, make the final d optional:
Character Classes and Ranges
Define a set or range of characters using square brackets:[abc]matchesa,b, orc[a-z]matches any lowercase letter[0-9]matches any digit
![The image shows a dark-themed interface with a command line prompt and examples of character ranges or sets, such as [a-z] and [0-9]. The text "KodeKloud" is visible in the corner.](https://kodekloud.com/kk-media/image/upload/v1752881478/notes-assets/images/Linux-System-Administration-for-Beginners-Extended-Regular-Expressions/dark-theme-command-line-character-sets.jpg)
Match cat or cut
Matching Device Files under /dev
A simple '/dev/.*' pattern is too greedy:
If device names include uppercase letters or multiple segments (e.g.,
/dev/tty0p0), use grouping and repetition to cover all cases.Sub-Expressions and Grouping
Parentheses() treat a group of tokens as a single unit. In arithmetic:

1 + 2 * 3 = 1 + (2×3) = 7(1 + 2) * 3 = 3×3 = 9
Alternation with |
Use | to match one pattern or another.
Negated Character Classes
Prefix a class with^ to invert it:
Beyond grep: Other Regex Tools
Most Linux utilities support ERE or similar syntax:| Tool | Use Case |
|---|---|
sed | Stream editing |
awk | Field-based text processing |
| Text editors | Interactive search & replace |