Comprehensive reference for regex patterns reference covering essential concepts, practical examples, and production best practices.
This reference exists for engineers who need to construct, debug, or deploy regex patterns as part of shell scripts, CI pipelines, configuration management, or log-processing workflows. It matters whenever a search, validation, or transformation step must be performed on text without invoking a full parser or external service. The sections that follow are organized by the problem you’re trying to solve—matching, extracting, validating, replacing, and debugging—rather than by regex syntax taxonomy. Every entry includes exact commands, flags, code snippets, and the precise error text you’ll encounter when a pattern goes wrong.
The most common entry point is searching a file for a fixed string or pattern. The flag choice determines whether the pattern is treated as extended regular expression syntax, Perl-compatible, or fixed text.
grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' changelog.txt – matches lines containing a semver version string at the start and end of the line. The -E flag enables extended regex, so + and \. are interpreted as quantifier and literal dot respectively.grep -P '^\d{4}-\d{2}-\d{2}\s' app.log – uses Perl-compatible regex to match ISO‑date formatted log timestamps. The -P flag is not available on all systems (notably some BSD variants); if absent, fall back to -E with equivalent syntax or install the pcre3 package.sed -i 's/old/new/g' file.txt – replaces every occurrence of old with new in place. If the replacement or search pattern contains /, the delimiter breaks. Use a different delimiter: sed -i 's|old|new|g' file.txt.sed -i 's/\(version=\)[0-9.]*/\16.2.1/' config.yaml – captures the prefix and substitutes only the version number. Parentheses create a capture group; the \1 backreference re-inserts the captured prefix. Omit the backslash in sed and the group is treated literally, resulting in s/\(version=\)... writing a literal \( into the file.perl -pi -e 's/old/new/g' file.txt – Perl one‑liner with in‑place editing. The -p flag prints each line after processing, -i edits the file directly, and -e supplies the expression. If the pattern contains special Perl metacharacters not escaped, the substitution silently replaces more than intended. Example: s/.+/REPLACED/ replaces the entire line content, not just the matched substring.grep: regex error: unmatched [ or ( – produced when a bracketed character class is opened without a closing ], or when ( is interpreted as a literal parenthesis instead of a group (use \\( or -E with proper escaping).Real‑world logs are rarely single‑line clean data; extracting a field often requires a combination of anchoring, capture groups, and tool‑specific flags.
2024-03-15T10:23:45Z INFO RequestID=abc123def456 latency=42ms src=10.0.1.7grep -oP '(?<=RequestID=)\S+' app.log – Perl‑compatible lookbehind extracts the value immediately following RequestID=. The -o flag outputs only the matching text, suppressing the whole line. If the system lacks -P, use grep -E 'RequestID=[^ ]+' app.log | cut -d= -f2.awk -F'[ =]' '{for(i=1;i<=NF;i++) if($i=="RequestID") print $(i+1)}' app.log – field‑split approach that works even when -P is unavailable. It iterates over space‑ or equals‑delimited fields and prints the token immediately after RequestID.abc123def456grep: unrecognized option (on systems where -P is misinterpreted as a flag) or simply no output and exit code 1 when the pattern fails to match any line.echo '{"host":"web01","status":"up"}' | grep -oP '"\K[^"]+(?=")' – the \K reset trick keeps the match start after the quote, and the positive lookahead " limits the capture to the value. Result: web01 on the first repetition, up on the second.Validation regexes are frequently embedded in scripts, configuration schemas, or CI step definitions. A precise pattern prevents invalid data from propagating downstream.
^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$echo "[email protected]" | grep -E '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$' returns the line (exit 0). echo "invalid" | grep -E '...' exits 1 with no output.This page was rewritten on 10 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy