Regex for PCRE, grep, ripgrep, and sed

Regex patterns and command-line workflows for grep, ripgrep, sed, awk, and advanced PCRE-style features.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

grep, egrep, and ripgrep

Common command-line regex search patterns.

grep whole word

Find full-word matches in files.

bashANYgrepripgrepsearch
bash
grep -RInw 'error' ./logs

`-R` recurses, `-I` skips binary, `-n` prints line numbers, and `-w` uses word boundaries.

ripgrep ignore case

Search recursively with ripgrep case-insensitively.

bashANYripgrepsearch
bash
rg -n -i 'timeout|timed out' .

ripgrep is fast and respects ignore files by default.

ripgrep with PCRE2

Use lookbehind or advanced features through PCRE2 mode.

bashANYripgreppcre2lookbehind
bash
rg -n -P '(?<=ERROR: ).+' app.log

`-P` enables PCRE2 for advanced constructs.

Invert match

Show lines that do not match a pattern.

bashANYgrepinvert
bash
grep -RInv '^\s*(#|$)' ./config

Useful for skipping comments and blank lines.

Alternation in egrep style

Search for one of several tokens.

bashANYgrepalternation
bash
grep -E 'warn|error|fatal' app.log

`grep -E` enables extended regular expressions.

sed regex replacement

Replace all runs of whitespace with a single space.

bashANYsedreplacement
bash
sed -E 's/[[:space:]]+/ /g' file.txt

Great for one-line normalization tasks.

awk regex condition

Print lines matching a regex in awk.

bashANYawkregex
bash
awk '/^[0-9]{4}-[0-9]{2}-[0-9]{2}/ { print }' file.txt

awk uses regex in pattern positions for line filtering.

PCRE and Advanced Features

Features common in PCRE-style engines and tools.

Atomic group in PCRE

Avoid backtracking inside a grouped token.

regexANYpcreatomic
regex
(?>[A-Z][a-z]+)\s+\d+

Useful when a branch should not be reconsidered.

Subroutine call

Reuse part of the pattern as a subroutine.

regexANYpcresubroutineadvanced
regex
^(?<pair>\((?:[^()]++|(?&pair))*\))$

Advanced PCRE technique for nested structures.

Recursion

Recursive pattern for balanced parentheses.

regexANYpcrerecursionadvanced
regex
^\((?:[^()]++|(?R))*\)$

Powerful but niche; useful in engines that support recursion.

Conditional expression

Branch based on whether a group matched.

regexANYpcreconditional
regex
^(?:(<))?\w+(?(1)>)$

Useful for optional paired delimiters in PCRE-like engines.

Keep out / skip fail

Skip over matched prefixes before continuing.

regexANYpcreskip-failadvanced
regex
\b(?:https?://\S+)(*SKIP)(*F)|\bTODO\b

Useful when excluding regions before matching the final target.

sed and Stream Editing

Regex-driven substitution and extraction with sed.

Reformat date

Convert YYYY-MM-DD into MM/DD/YYYY.

bashANYsedcapturesdate
bash
sed -E 's/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/\2\/\3\/\1/'

Uses capture groups and backreferences in the replacement.

Trim trailing spaces

Remove trailing whitespace from each line.

bashANYsedcleanupwhitespace
bash
sed -E 's/[[:space:]]+$//' file.txt

Useful before committing source files.

Prefix non-empty lines

Add a prefix only to non-empty lines.

bashANYsedline-processing
bash
sed -E '/^$/! s/^/# /' notes.txt

Combines a regex address with substitution.

Extract quoted content

Print only the content inside double quotes on each line.

bashANYsedextract
bash
sed -En 's/.*"([^"]+)".*/\1/p' file.txt

The `-n` flag suppresses normal printing; `p` prints only successful substitutions.

Recommended next

No recommendations yet.