Regular expressions, often shortened to regex, describe text patterns. Tools such as grep, sed, and awk use regex, but their supported syntax can differ, so always identify the tool and regex flavor.
Advanced Text-Fu · Lesson 1
regex (Regular Expressions)
Learn how anchors, character sets, repetition, and regex flavors control text pattern matching.
GNU grep uses basic regular expressions (BRE) by default and extended regular expressions (ERE) with -E. This lesson introduces constructs shared by both, then notes common ERE additions.
Use this input in the examples:
sally sells seashells
by the seashore
Matching Literal Text
Most ordinary characters match themselves. The pattern seashells selects a line containing that exact sequence anywhere:
$ grep 'seashells' sample.txt
sally sells seashells
Quote regex patterns so the shell does not expand or split them before the matching tool receives them. Regex is also different from shell pathname expansion: in a regex, * repeats the preceding atom; in a shell glob, * is itself a wildcard for a string of pathname characters.
What does * do in a regular expression such as ab*?
Anchoring a Match
Outside a bracket expression, ^ at the beginning of a pattern anchors the match at the beginning of a line:
^by
The $ anchor matches at the end of a line:
seashore$
Combine both anchors when the entire line must fit the pattern:
^by the seashore$
Which pattern matches only a line whose complete text is by the seashore?
Matching One Character
The dot matches one character in ordinary line-oriented regex mode:
b.
This matches by, but it could also match ba or b7. It does not match a lone b because one character is required after it. To match a literal period, escape it as \. or place it in a suitable bracket expression.
Which string is not matched by the complete-line pattern ^b.$?
Using Bracket Expressions
A bracket expression matches one character from a specified set:
s[ae]lls
This matches sells or salls at that position.
When ^ is the first character after [, it negates the set:
s[^e]lls
This matches salls but not sells because the character after the first s cannot be e.
What does [^e] match?
Ranges can describe characters between endpoints:
d[a-c]g
This can match dag, dbg, or dcg. Range behavior can depend on locale collation. Character classes such as [[:lower:]], [[:upper:]], and [[:digit:]] often express intent more clearly.
Repeating and Combining Patterns
In both BRE and ERE, * means zero or more repetitions of the preceding atom:
seashells*
This matches seashell followed by zero or more additional s characters. In ERE mode with grep -E, common operators include:
+: One or more repetitions.?: Zero or one repetition.|: Either the expression on the left or the right.(...): Group expressions.
For example:
$ grep -E '^(cat|dog)s?$' animals.txt
This selects complete lines equal to cat, cats, dog, or dogs. In BRE mode, these operators have different escaping rules, so do not copy a pattern between flavors without checking it.
Which command enables extended regex syntax for the pattern ^(cat|dog)s?$?
To practice regex selection with Linux text tools, try these hands-on labs:
- Search Text with grep in Linux - In this lab, you will learn to search for text in files on a Linux system using the
grepcommand. You will perform basic searches, display line numbers, use anchors like^and$to match line positions, and harness both basic and extended regular expressions for complex pattern matching. - Text Processing and Regular Expressions - Learn the powerful text processing tools grep, sed, and awk. Learn to use regular expressions for efficient text manipulation and pattern matching in Linux.
- Extracting Mails and Numbers - In this challenge, you will learn how to use grep and regular expressions to extract email addresses and numbers from a file, demonstrating essential Linux text processing skills.
Lesson complete
You finished regex (Regular Expressions)
You can now read and build foundational line-oriented regular expressions.
Distinguish regex operators from shell pathname wildcards.
Anchor matches at the beginning or end of a line.
Match one character with a dot or bracket expression.
Negate sets and use locale-aware character classes.
Choose BRE or ERE syntax deliberately.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account