Regular Expressions (Regex) — Pattern Matching in Linux¶
Regular Expressions (Regex) are a powerful pattern-matching language used to search, filter, validate, and manipulate text. They are the foundation of commands like
grep,sed, andawk, and are widely used in programming languages, log analysis, DevOps automation, cybersecurity, and system administration.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand Regular Expressions
- Match exact text
- Use anchors
- Work with wildcards
- Create character classes
- Use quantifiers
- Apply grouping
- Build advanced search patterns
- Use Regex with grep, sed, awk, and find
- Analyze production logs
Prerequisites¶
Complete:
- Module 1
- Module 2
- Module 3 Lessons 1–17
Why Learn Regex?¶
Suppose a production log contains 10 million lines.
You need to find:
- IP addresses
- Email addresses
- URLs
- Failed login attempts
- Error codes
- Kubernetes Pod names
- Docker image tags
Without Regex:
Impossible.
With Regex:
Done in seconds.
What is a Regular Expression?¶
A Regular Expression (Regex) is a pattern used to match text.
Instead of searching for one word:
Regex lets you search for patterns such as:
Sample File¶
Create:
Contents:
Exact Match¶
Matches:
Dot (.)¶
Matches any single character.
Pattern:
Matches:
Also matches:
Beginning of Line (^)¶
Matches:
End of Line ($)¶
Matches only lines ending with:
Character Classes¶
Any digit.
Lowercase.
Uppercase.
Letters.
Letters and digits.
Negated Character Class¶
Anything except digits.
Wildcard *¶
Zero or more.
Matches:
Plus (+)¶
One or more.
Matches:
Requires:
Question Mark (?)¶
Optional.
Matches:
Curly Braces {}¶
Exactly three digits.
Between 2 and 5.
OR Operator¶
Grouping¶
Matches:
Word Boundary¶
Matches:
Not:
Common POSIX Character Classes¶
| Pattern | Meaning |
|---|---|
[[:digit:]] | Digits |
[[:alpha:]] | Letters |
[[:alnum:]] | Letters and digits |
[[:space:]] | Whitespace |
[[:upper:]] | Uppercase |
[[:lower:]] | Lowercase |
Search Numbers¶
Search Uppercase¶
Search Emails¶
Example:
Search IPv4 Addresses¶
Search URLs¶
Search Kubernetes Pods¶
Search Docker Images¶
Regex with grep¶
Regex with sed¶
Replace all digits.
Regex with awk¶
Regex with find¶
Production Examples¶
Failed SSH login.
IP addresses.
Emails.
Container image.
YAML values.
Production Perspective¶
Regex is used in:
- Linux
- Kubernetes
- Docker
- Git
- GitLab
- Jenkins
- Terraform
- Ansible
- Python
- Java
- Go
- Security
- SIEM
- Splunk
Learning Regex once benefits almost every technology you use.
Hands-on Lab¶
Task 1¶
Search uppercase.
Task 2¶
Search numbers.
Task 3¶
Search email.
Task 4¶
Search IP.
Task 5¶
Replace digits.
Task 6¶
Engineering.
Task 7¶
Search URLs.
Task 8¶
Beginning and end.
Regex Cheat Sheet¶
| Symbol | Meaning |
|---|---|
. | Any character |
^ | Beginning of line |
$ | End of line |
* | Zero or more |
+ | One or more |
? | Optional |
[] | Character class |
[^] | Negated class |
{} | Repetition |
| | OR |
() | Group |
Production Troubleshooting Scenario¶
Scenario
An SRE receives a 5 GB application log.
Tasks:
- Find ERROR and WARNING messages.
- Extract IP addresses.
- Extract email addresses.
- Find Kubernetes image tags.
- Count failed logins.
Commands:
grep -E "ERROR|WARNING"
grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}"
grep -E "[A-Za-z0-9._%+-]+@"
grep "^image:"
grep "Failed password" | wc -l
Best Practices¶
- Use
grep -Efor extended regular expressions. - Test Regex on sample files before production use.
- Keep patterns readable and well-documented.
- Avoid overly broad patterns that may produce false positives.
- Build complex expressions incrementally.
Common Mistakes¶
❌ Confusing shell wildcards (*, ?) with Regex.
✅ Shell globbing and Regex are different.
❌ Forgetting -E.
✅ Patterns using:
generally require:
❌ Creating patterns that are too greedy.
✅ Use the simplest pattern that matches the intended data.
Interview Questions¶
Beginner¶
- What is a Regular Expression?
- What does
.match? - What does
^represent? - What does
$represent?
Intermediate¶
- Difference between
*and+. - Explain character classes.
- What is
grep -E? - How do you match an email address?
Architect Level¶
- How would you analyze a multi-gigabyte production log using Regex?
- Why are Regular Expressions essential in DevOps automation?
- How do you balance Regex flexibility with readability and maintainability?
Summary¶
In this lesson, you learned:
- Regex fundamentals
- Anchors
- Character classes
- Quantifiers
- Grouping
- Alternation
- Pattern matching
- Real-world production examples
Regular Expressions are one of the most valuable skills in Linux and software engineering. Once mastered, they become a powerful tool for searching, validating, extracting, and transforming data across countless technologies.
Key Takeaways¶
- Regex matches patterns, not just literal text.
- Learn anchors (
^,$) and character classes ([]) early. - Use
grep -Efor extended Regex features. - Regex is used throughout Linux, DevOps, cloud platforms, and programming languages.
- Practice regularly—the best way to master Regex is by solving real-world text-processing problems.
Module 3 Completed!¶
Congratulations! You have mastered Linux text processing, including:
grepcutsortuniqtrwcpastejoinsplitfmtcolumnstringsteexargssedawk- Regular Expressions (Regex)
You are now equipped with the core text-processing skills used daily by Linux Administrators, DevOps Engineers, SREs, Cloud Architects, and Security Professionals.
Next Module¶
Module 4 – File Management and Permissions
Start with File Types in Linux, then continue with links, permissions, ownership, umask, ACLs, and secure file operations.