How to Learn Regular Expressions Step by Step the Right Way
Learn regular expressions in seven small steps, from literal text to groups, each with a command you can run and a short exercise with its answer.

Regular expressions look like line noise until you see that there are only about a dozen ideas in them, and each one is small. The mistake is learning them from a cheat sheet of fifty symbols. Learn them as seven steps, and write each one yourself on real text. The practice tool is grep -E, which every Linux and macOS machine has. It is the starting point of regular expressions in shell tools.
All the commands run in GNU grep 3.11, and also in BSD grep for everything shown except where noted. Use -o to print only what matched, so you can see the pattern's effect.
Step 1: literal text
printf 'cat\nconcatenate\ndog\n' | grep -o 'cat'Most characters match themselves. This prints cat twice, once from each line that contains it. A regex is a search for text, with special characters added for flexibility.
Step 2: the dot
printf 'cat\ncot\ncut\ncart\n' | grep -oE 'c.t'. matches any single character, so this prints cat, cot and cut, and not cart, which has two characters between c and t. To match a real dot, escape it as \..
Step 3: character classes
printf 'a1\nb22\nc\n' | grep -oE '[a-z][0-9]'Square brackets match one character from a set: [abc], a range [a-z], or the opposite [^0-9] (anything but a digit). Named classes like [[:digit:]] are the portable form, covered in POSIX character classes.
Step 4: how many
| Quantifier | Means |
|---|---|
? | Zero or one |
* | Zero or more |
+ | One or more |
{3} | Exactly three |
{2,4} | Two to four |
printf 'room 7\nroom 42\nroom\n' | grep -oE '[0-9]+'The quantifier applies to the thing just before it. This prints 7 and 42. Quantifiers are greedy by default, which causes the commonest surprise; see how greedy and lazy matching work.
Step 5: anchors
printf 'error: disk\nno error here\n' | grep -E '^error'^ is the start of a line and $ the end. Only the first line matches. Anchors turn "contains" into "starts with" or "is exactly", and are described in anchors and word boundaries.
Step 6: groups and alternatives
printf 'color\ncolour\nclr\n' | grep -E '^colou?r$'
printf 'cat\ndog\nbird\n' | grep -E '^(cat|dog)$'Parentheses group parts so a quantifier applies to all of them, and | separates alternatives. Groups can also be captured and reused, as backreferences and capture groups shows.
Step 7: escaping
Characters with special meaning (. * + ? ( ) [ ] { } | ^ $ \) lose it when preceded by a backslash. To find the text 3.14, write 3\.14. In a bracket expression most of them are literal already: [.] is a dot. When your pattern is a plain string from a variable, skip regex entirely with grep -F.
Practice
Write patterns for these, then compare with the answers.
- A line that is only digits. Answer:
^[0-9]+$ - A time like
09:30. Answer:[0-9]{2}:[0-9]{2} - The word
grayorgrey. Answer:gr(a|e)yorgr[ae]y - A line that starts with a hash. Answer:
^#
How the engine reads a pattern
A regular expression is a tiny program that the engine runs against your text, one starting position at a time. At each position it tries to match the pattern from left to right. If it fails, it moves one character along and tries again. When it succeeds, it reports the match and carries on after it. Understanding that loop answers most of the questions beginners ask: why a pattern finds the first of several candidates, why an anchor changes where it can start, and why a pattern that is too general matches things you did not expect. The commands below use GNU grep 3.11 on Debian 13, with -o to print only what matched.
printf 'abc 123 _x\n' | grep -oE '[[:digit:]]+'
printf 'a1\nb22\nc\n' | grep -oE '[a-z][0-9]'
printf 'color\ncolour\nclr\n' | grep -E '^colou?r$'123
a1
b2
color
colour
The second command shows the engine's behavior. The line b22 contains a letter followed by two digits, and the pattern asks for a letter followed by one digit, so it matched b2 and stopped. The remaining 2 was not a letter, so nothing more matched. Without the anchors in the third command, colou?r would also match inside a longer word.
Escaping: the dot that is not a dot
Characters such as the dot, the star, the plus and the brackets have special meaning, and a backslash turns them back into plain characters. Forgetting this gives patterns that match too much, and the failure is silent because the pattern still finds the text you wanted. A dot in x.y matches any character between the x and the y.
echo 'x.y' | grep -E 'x.y'
echo 'xzy' | grep -cE 'x.y'
echo 'xzy' | grep -cE 'x\.y'x.y
1
0
The unescaped dot matched the letter z in xzy, which you did not intend if you were looking for a file extension or a version number. The escaped form matched only a literal dot. When you search for text that contains many special characters, grep -F avoids the problem by turning off the pattern language altogether.
Build a pattern up, not down
The practical habit is to start with the smallest pattern that matches one example and extend it, checking each step against both lines that should match and lines that should not. Keep a small file of positive and negative examples next to the pattern. When the pattern changes, run it over both sets and diff the result. That is a test suite in two files, and it catches the regression the next time someone makes the pattern "a bit more general". A regular expression with no examples beside it is a liability, because nobody dares to change it.
Keep a practice file
Build a small file of test lines that grows as you learn: some that you want to match, some that you do not, and some tricky ones with punctuation, uppercase letters and repeated words. Run each new pattern against the whole file and look at the result. This is the fastest way to see what a pattern really does, since a regular expression read in isolation tells you little. Online testers help, but check which dialect they use, because a pattern that works in a tester for JavaScript may not work in grep. Keep the practice file as a reference for patterns you wrote and liked, with a short note on what each one does. In six months it will be a library of solutions.
printf 'cat\nconcatenate\nCat\ncat food\nbobcat\n' > practice.txt
grep -c 'cat' practice.txt
grep -ciw 'cat' practice.txtThe first command counts every line containing the letters, including concatenate and bobcat. The second counts whole words only, case-insensitively, and the difference between the two numbers is the lesson of the anchors and boundaries in the steps above.
Where the learning curve steepens
Different tools use slightly different regex dialects, so a pattern that works in grep -E may need changes in Python or sed; why regex matches differ across tools lists the traps. For a longer treatment, the Python documentation has a good regular expression HOWTO, and POSIX defines the shell-tool dialects in Regular Expressions.


