Skip to main content
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.

Using grep -E vs egrep

Both grep -E and egrep enable ERE syntax, so you don’t need to escape +, ?, {}, or |:
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:

Examples


? and * Quantifiers

  • x?zero or one x
  • x*zero or more x
To match both disable and disabled, make the final d optional:

Character Classes and Ranges

Define a set or range of characters using square brackets:
  • [abc] matches a, b, or c
  • [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.

Match cat or cut


Matching Device Files under /dev

A simple '/dev/.*' pattern is too greedy:
To restrict matches to letters plus an optional digit:
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:
The image shows a dark interface with a command line prompt on the left and a calculation on the right, demonstrating the expression "1+2*3" which equals "7".
  • 1 + 2 * 3 = 1 + (2×3) = 7
  • (1 + 2) * 3 = 3×3 = 9
In regex:

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: For interactive testing, try Regexr.

References

Happy pattern crafting!

Watch Video

Practice Lab