Regular Expressions (Regex) Cheat Sheet

A Regular Expression (Regex) is a sequence of characters that specifies a search pattern in text. They are ubiquitous across Linux CLI tools (grep, sed, awk), programming languages, log parsing filters (Vector, FluentBit), and WAF security rules.


1. Character Classes & Metacharacters

PatternMatchesEquivalent Set
.Any character except newline (\n)[^\n]
\dAny digit (0-9)[0-9]
\DAny non-digit[^0-9]
\wAny word character (alphanumeric + underscore)[a-zA-Z0-9_]
\WAny non-word character[^a-zA-Z0-9_]
\sAny whitespace (space, tab, newline)[ \t\r\n\f]
\SAny non-whitespace character[^ \t\r\n\f]
[abc]Any character inside the brackets (a, b, or c)Explicit Set
[^abc]Any character NOT inside the bracketsNegated Set
[a-z]Range: any lowercase character from a to zRange

2. Anchors & Boundaries

PatternMeaningExample
^Start of line (or start of string)^Error (Matches line starting with “Error”)
$End of line (or end of string)\.log$ (Matches files ending with “.log”)
\bWord boundary\bcat\b (Matches “cat”, but not “concatenate”)
\BNon-word boundary\Bcat\B (Matches inside “concatenate”)

3. Quantifiers (Greedy vs Lazy)

QuantifierDescriptionBehavior
*0 or more timesGreedy (matches as many as possible)
+1 or more timesGreedy
?0 or 1 time (optional)Greedy
{n}Exactly timesExact
{n,}At least timesGreedy
{n,m}Between and timesGreedy
*?0 or more timesLazy (matches the fewest characters possible)
+?1 or more timesLazy

4. Groups, Alternation & Lookarounds

SyntaxTypeDescription
(abc)Capturing GroupGroups tokens together and creates backreference $1
(?:abc)Non-Capturing GroupGroups tokens without storing backreference (faster)
(?<name>abc)Named GroupAssigns a key name to captured match
a|bAlternationMatches either a or b
(?=...)Positive LookaheadAsserts that pattern follows, without including in match
(?!...)Negative LookaheadAsserts that pattern does NOT follow
(?<=...)Positive LookbehindAsserts that pattern precedes match
(?<!...)Negative LookbehindAsserts that pattern does NOT precede match

5. Battle-Tested DevOps Regex Recipes

IPv4 Address (Valid Octets 0-255)

\b(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?:\.(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}\b

UUIDv4 Format

\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}\b

ISO 8601 Timestamp (YYYY-MM-DDTHH:MM:SSZ)

^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$

Semantic Versioning (SemVer)

^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$