Regular Expressions
What are Regular Expressions?
Regular expressions (RegEx or regex for short) is a pattern matching syntax for describing text patterns. It's used for text search, matching, replacement, and other operations, and is a core skill in Linux text processing.
Regex Types
Linux has two main regular expression types:
Main Differences
Basic Meta Characters
Character Matching
Character Class Shortcuts
POSIX Character Classes
Position Anchors
Quantifiers
Groups and Capturing
Alternation (OR)
Common Pattern Examples
Numbers
Strings
Using grep with Regex
Examples
Using sed with Regex
Practical Examples
Using awk with Regex
Greedy vs Non-greedy
By default, regular expressions are greedy and will match as much as possible.
Common Errors
1. Forgetting to Escape
2. BRE and ERE Confusion
3. Greedy Matching Issues
Testing Tools
Online Tools
- regex101.com
- regexr.com
Command Line Testing
Summary
This chapter introduced Linux regular expressions:
- Meta characters:
.,*,+,?,^,$ - Character classes:
[],[^], POSIX classes - Quantifiers:
{n},{n,m},{n,},{n,m} - Groups and capturing:
(),\1,\2 - Alternation:
| - In tools: grep, sed, awk
Regular expressions require practice to master. Start with simple patterns and gradually learn more complex usage.
Previous chapter: Text Processing Tools
Next chapter: User Management