What are regular expressions and where are they used in Linux?
Regular expressions are mini-patterns that describe sets of strings, used by text tools (grep, sed, awk) to search and match instead of looking for one fixed string.
A regex lets you say "any line starting with a digit" or "an email-looking thing" rather than one literal word. The building blocks combine a few metacharacters — . (any char), * (repeat), ^/$ (line anchors), [ ] (character sets) — into surprisingly expressive patterns. They show up everywhere a tool filters or transforms text, which on Linux is nearly everywhere: log analysis, config parsing, search-and-replace.
Where used:
grep- Search in filessed- Stream editingawk- Text processing- Programming languages (Python, Perl, etc.)
Basic regex components:
| Pattern | Matches |
|---|---|
. |
Any single character |
* |
Zero or more of previous |
^ |
Start of line |
$ |
End of line |
[ ] |
Character class |
[^ ] |
Negated character class |
Example with grep:
# Find lines starting with "error"
grep "^error" logfile.txt
# Find lines ending with numbers
grep "[0-9]$" data.txt
# Find "cat" or "cut" or "cot"
grep "c.t" words.txt
Gotcha — two regex dialects: in basic regex (plain grep), + ? | ( ) are literal characters; you'd have to escape them as \+. Extended regex (grep -E) treats them as operators directly. When a pattern "doesn't work," a mismatched dialect is the usual culprit — try adding -E.
Go deeper:
Regular expression — Wikipedia — metacharacters, quantifiers, anchors, and the POSIX BRE/ERE distinction.
grep(1) man page (man7.org) — which metacharacters lose meaning in basic vs extended regex.