Practice Examples¶
Try these exercises to solidify your understanding:
Practice Exercises¶
Learning by Doing
Try each exercise in order. The expected output is shown to help you verify your results.
graph LR
Start[Start Here] --> Basic["Exercise 1
Basic Search"]
Basic --> FlagsBox["Exercises 2-6
Search Flags"]
FlagsBox --> AdvBox["Exercises 7-8
Advanced Patterns"]
AdvBox --> FilterBox["Exercises 9-10
Filtering"]
FilterBox --> OutputBox["Exercise 11
Output Format"]
OutputBox --> Challenge["Exercise 12
Advanced Challenge"]
Challenge --> Practice[Practice More]
style Start fill:#e8f5e9
style Basic fill:#e1f5ff
style FlagsBox fill:#fff3e0
style AdvBox fill:#f3e5f5
style FilterBox fill:#fce4ec
style OutputBox fill:#e0f2f1
style Challenge fill:#ffebee
style Practice fill:#e8f5e9
Figure: Learning progression showing how exercises build from basic to advanced techniques.
1. Basic Search¶
Find all TODO comments in your project:
2. Case-Insensitive Search¶
Find all error messages (any case):
Tip
Learn more about case sensitivity options in Case Sensitivity.
Expected Output
3. Literal Search¶
Find function calls to "log()" (literal match, not regex):
Note
The -F flag treats the pattern as a literal string, so special regex characters like () are matched exactly. Learn more in Literal Search.
4. Word Boundaries¶
Find variable named "id" (not "valid", "identity", etc.):
Tip
Learn more about word boundaries and anchors in Boundaries.
5. Count Matches¶
Count how many times each file uses "import":
Tip
Learn more about counting and listing matches in Count and List.
Note
Files with zero matches are not shown by default.
6. Context Lines¶
Find errors with surrounding context:
Expected Output
7. Regex Pattern¶
Find hexadecimal numbers:
- Pattern breakdown:
0xmatches literal "0x" prefix,[0-9a-fA-F]matches any hex digit (0-9, a-f, A-F),+matches one or more digits
Tip
Learn more about regex patterns in Pattern Matching and Regex Basics.
8. Inverted Match¶
Find all non-empty lines:
Note
This is useful for filtering out blank lines from output or counting lines of actual content.
9. File Type Filtering¶
Find imports only in Rust files:
Tip
Use rg --type-list to see all available file types. Learn more in File Filtering.
10. Multiline Search¶
Find struct definitions that span multiple lines:
- Pattern breakdown:
-Uenables multiline mode,struct \w+matches "struct" followed by a name,\s*\{matches optional whitespace and opening brace,[^}]+matches one or more non-brace characters (the struct body),\}matches closing brace
Expected Output
Advanced Pattern
The -U flag enables multiline mode. Source: tests/multiline.rs
11. JSON Output¶
Get structured JSON output for parsing:
Expected Output
Tip
JSON output is useful for integrating ripgrep into tools and scripts. Learn more in Output Formatting.
12. Advanced Challenge¶
Combine multiple concepts - search for TODO comments in Rust files only, case-insensitive, with 2 lines of context, output as JSON:
Expected Output
{"type":"context","data":{"path":{"text":"src/main.rs"},"lines":{"text":"fn main() {\n"},"line_number":13,"absolute_offset":280,"submatches":[]}}
{"type":"match","data":{"path":{"text":"src/main.rs"},"lines":{"text":" // TODO: Implement error handling\n"},"line_number":15,"absolute_offset":342,"submatches":[{"match":{"text":"TODO"},"start":7,"end":11}]}}
{"type":"context","data":{"path":{"text":"src/main.rs"},"lines":{"text":" process();\n"},"line_number":16,"absolute_offset":378,"submatches":[]}}
Challenge Yourself
Try modifying this command to:
- Exclude test files: add
-g '!*test*' - Only show file names: replace
--jsonwith-l - Count matches per file: replace
-C 2 --jsonwith-c
Common Mistakes¶
flowchart TD
Start[Need to Search?] --> Special{"Contains
special chars?
. * + ? etc."}
Special -->|Yes| Literal["Use -F flag
for literal search"]
Special -->|No| Case{"Case
matters?"}
Case -->|No| CaseFlag["Use -i flag
case-insensitive"]
Case -->|Yes| Whole{"Match whole
words only?"}
Whole -->|Yes| Word["Use -w flag
word boundaries"]
Whole -->|No| Hidden{"Search
hidden files?"}
Hidden -->|Yes| HiddenFlag["Use --hidden
--no-ignore"]
Hidden -->|No| Type{"Specific
file types?"}
Type -->|Yes| TypeFlag["Use -t flag
file type filter"]
Type -->|No| Search[Run search]
Literal --> Search
CaseFlag --> Search
Word --> Search
HiddenFlag --> Search
TypeFlag --> Search
style Start fill:#e8f5e9
style Special fill:#fff3e0
style Literal fill:#e1f5ff
style Search fill:#c8e6c9
Figure: Decision flowchart for choosing the right ripgrep flags to avoid common mistakes.
Forgetting to Escape Regex Metacharacters
Problem:
# This searches for regex pattern "file.txt", which matches "file_txt", "fileXtxt", etc.
rg "file.txt"
Solution:
Case Sensitivity Confusion
Problem:
Solution:
Matching Partial Words
Problem:
Solution:
Hidden Files Not Searched by Default
Problem:
Solution:
# Use --hidden to search hidden files
rg --hidden "SECRET_KEY"
# Use --no-ignore to also search ignored files
rg --hidden --no-ignore "SECRET_KEY"
Note
ripgrep respects .gitignore by default for better performance. Source: crates/ignore/src/lib.rs
Binary Files Skipped Automatically
Problem:
Solution:
# Use -a to search binary files (may produce garbled output)
rg -a "pattern"
# Use --binary to search binary files but show only filenames
rg --binary "pattern"
Tip
For searching specific binary formats, consider specialized tools or convert to text first.