Read

Module 4 · Finding things

Regular expressions, part 2: classes and groups with egrep

Character classes like [0-9], alternatives with |, grouping with ( ) and the +, ? and {n} repetitions of extended regular expressions.

What you will learn

  • Write character classes with brackets, ranges, negation and the POSIX names like [:digit:].
  • Use grep -E (egrep) for |, ( ), +, ? and {n,m}.
  • Tell basic from extended syntax and know when each applies.

The dot matches any character, which is often too generous. Character classes let you say "a digit", "a vowel" or "anything but a space". And when you want "this or that", or to repeat a whole group rather than a single character, you need the extended regular expression syntax, which grep -E enables; egrep is a traditional alias for the same thing, and this VM has both.

Character classes

Square brackets match one character from the set inside. [aeiou] is a vowel, [0-9] a digit, [a-zA-Z] a letter, [^0-9] anything that is *not* a digit (the caret negates when it is first inside the brackets). POSIX names make this readable and locale-safe: [[:digit:]], [[:alpha:]], [[:space:]], [[:upper:]]; the double brackets are correct, the inner pair is the class name and the outer pair the set. Inside brackets most special characters lose their meaning, so [.] is a literal dot.

Classes work in basic grep too, and combine with *: grep '^[0-9]*

TempMV — Temporary Virtual Machines in your browser

TempMV creates, boots and throws away real temporary x86 virtual machines directly in your browser — nothing to install, no account needed. It uses the v86 hypervisor (WebAssembly) to run real machine code. Reload the page and the machines power off and disappear: they are throwaway / disposable by design.

También en español: TempMV crea y desecha máquinas virtuales temporales y efímeras en el navegador, sin instalar nada y sin necesidad de cuenta.

TempMV needs JavaScript and WebAssembly enabled to run.

matches lines made only of digits (including empty ones, because * allows zero). grep '[A-Z]' finds lines with at least one capital letter, and grep -v '[^a-z]' finds lines with nothing but lowercase letters.

Extended syntax: |, ( ), +, ?, {n,m}

SyntaxMeaningExample (grep -E)
a\|ba or bgmail\|yahoo
( )group, to apply | or a repetition to several characters^(GET\|POST)
+one or more of the preceding item[0-9]+ a whole number
?zero or one (optional)colou?r
{n} {n,} {n,m}exactly n / at least n / between n and m[0-9]{3} three digits

The difference between + and * matters: [0-9]* can match nothing at all, so it never filters lines out; [0-9]+ requires at least one digit. Groups make repetitions apply to sequences: (ha)+ is one or more ha, (ha){2,} is haha or longer. | has the lowest precedence, so ^cat|dog$ means "starts with cat, OR ends with dog"; write ^(cat|dog)$ for "exactly cat or exactly dog".

~% cd /root/lab/l34
l34% cat contacts.txt
ana ana@gmail.com 600123456
luis luis@yahoo.es
maria maria@empresa.org 912345678
pepe no-email
l34% grep '[0-9]' contacts.txt
ana ana@gmail.com 600123456
maria maria@empresa.org 912345678
l34% grep -E 'gmail|yahoo' contacts.txt
ana ana@gmail.com 600123456
luis luis@yahoo.es
l34% grep -oE '[0-9]{9}' contacts.txt
600123456
912345678
l34% grep -E '@[a-z]+\.(com|org)' contacts.txt
ana ana@gmail.com 600123456
maria maria@empresa.org 912345678
l34% grep -E '(ha){2,}' feelings.txt
haha!
hahaha

Basic versus extended

In basic syntax (plain grep, sed) the characters + ? | ( ) { } are ordinary, and you must escape them to get the special meaning: \(ha\)\{2,\}. In extended syntax (grep -E, egrep, sed -E, awk) they are special as written. Both understand . * ^ $ [ ] the same way. When a pattern needs any of the extended operators, just reach for -E; there is no downside.

Commands in this lesson

CommandWhat it does
grep '[0-9]' fileLines containing a digit.
grep '[^a-z]' fileLines containing a non-lowercase character.
grep '[[:upper:]]' filePOSIX class: lines with a capital letter.
grep -E 'cat|dog' fileLines with cat or dog.
grep -E '^(GET|POST) ' fileGroup the alternatives.
grep -E '[0-9]+' fileOne or more digits.
grep -E 'colou?r' fileOptional character.
grep -oE '[0-9]{3}' filePrint just the three-digit matches.

Quiz

  1. What does `[0-9]` match?

    • The text `0-9`
    • Exactly one digit
    • Any number of digits
  2. Which command finds lines containing `error` or `warning`?

    • `grep 'error|warning' log`
    • `grep -E 'error|warning' log`
    • `grep 'error warning' log`
  3. What is the difference between `[0-9]*` and `[0-9]+`?

    • None
    • `*` allows zero digits, so it matches every line; `+` requires at least one
    • `+` only works with letters
  4. What does `grep -E '^(yes|no)

    TempMV — Temporary Virtual Machines in your browser

    TempMV creates, boots and throws away real temporary x86 virtual machines directly in your browser — nothing to install, no account needed. It uses the v86 hypervisor (WebAssembly) to run real machine code. Reload the page and the machines power off and disappear: they are throwaway / disposable by design.

    También en español: TempMV crea y desecha máquinas virtuales temporales y efímeras en el navegador, sin instalar nada y sin necesidad de cuenta.

    TempMV needs JavaScript and WebAssembly enabled to run.

    ` match?

    • Lines that are exactly `yes` or exactly `no`
    • Lines starting with yes or ending with no
    • Lines containing `yes|no`
  5. Which pattern matches a Spanish postcode, exactly five digits, as a whole line?

    • `grep -E '^[0-9]{5} `
    • `grep -E '[0-9]5'`
    • `grep '^[0-9]* `

Practice

  1. /root/lab/l34/contacts.txt has one contact per line; some include a phone number. Save the lines that contain at least one digit into /root/lab/l34/digits.txt.

  2. Save into /root/lab/l34/mail.txt the lines of /root/lab/l34/contacts.txt that contain either `gmail` or `yahoo`, using a single pattern with an alternative.

  3. /root/lab/l34/feelings.txt has short lines such as `ha`, `hat`, `haha!` and `hahaha`. Using a group with a repetition count, save the lines where `ha` appears two or more times in a row into /root/lab/l34/laugh.txt.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.