07. Extended Regular Expressions

Up until now we have covered all the characters used under basic regular expressions (BRE). Here, we'll go over extended regular expressions (ERE), which can be used with grep's -E option.

Similarities and Differences

Here is a list of similarities and differences between ERE's and BRE's:

  • In ERE's there are no backreferences, although the parentheses have special meanings.
  • Same notation for matching single characters
  • No need for escape keys when uses braces ({n, m} or parentheses.


Rather than symbols for backreferences, parentheses in ERE are used for groupings. This allows you to specify groups of text as regular expressions, which is helpful for metacharacters to reference a group of previous regex.

For example, (toy)* would select the letters toy zero or many times.

Extended features

There are three extended metacharacters that ERE offers.

Question mark (?)

The ? matches zero or one of the preceding regex.

Plus sign (+)

The plus symbol (+) is used to match one or more of the preceding regex. It's very similar to the asterisk (*), but allows for no NUL values.

$ ls /usr/bin | grep -E '[[:alpha:]][[:digit:]]+[[:alpha:]]'

This would select all text with a letter, followed by one or more digits, then another letter. We could have gotten the same result with [[:alpha:]][[:digit:]][[:digit:]]*[[:alpha:]] but the previous example looks must cleaner.

Alternations (|)

Alternations give you the flexibility of choosing between two or more regex expressions.

$ ls /usr/bin/ | grep -E 'zip|pod|enc'
bunzip2 bzip2 bzip2recover enc2xs enc2xs5.16 enc2xs5.18 encode_keychange funzip gencat gunzip gzip latency pod2html pod2html5.16 podchecker5.18 podselect podselect5.16 podselect5.18 unzip unzipsfx uuencode weblatency.d zip zipcloak zipdetails zipdetails5.16 zipdetails5.18 zipgrep zipinfo zipnote zipsplit

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources