Regex#
Regular expressions: the operator’s general-purpose pattern
language. Reach for it any time the goal is to find, validate,
extract, or rewrite text by structure rather than by exact
string. The same syntax works (with documented quirks) across
grep / rg / sed / awk on the shell, the
re / regex modules in Python, RegExp in JavaScript,
Pattern in Java, Regex in .NET, regexp in Go /
Rust, YARA rules in malware tooling, and most editors.
This page is structured for lookup: read top-down to learn, or jump to one section by header. Engine-specific notes are called out inline; the engine-comparison section maps which features port across implementations.
Anchors#
Anchor |
Description |
Example |
Valid |
Invalid |
|---|---|---|---|---|
|
Start of string or line. |
|
|
|
|
Start of string in any match mode. |
|
|
|
|
End of string or line. |
|
|
|
|
End of string, or char before last new line, in any match mode. |
|
|
|
|
End of string in any match mode. |
|||
|
End of previous match, or start of string for the first match. |
|
|
|
|
Word boundary, between |
|
|
|
|
Not a word boundary. |
|
|
|
Assertions#
Assertion |
Description |
Example |
Valid |
Invalid |
|---|---|---|---|---|
|
Positive lookahead. |
|
|
|
|
Negative lookahead. |
|
|
|
|
Positive lookbehind. |
|
|
|
|
Negative lookbehind. |
|
|
|
Character class#
Class |
Description |
Example |
Valid |
Invalid |
|---|---|---|---|---|
|
Class definition. |
|
|
|
|
Class definition range. |
|
|
|
|
Escape inside class. |
|
|
|
|
Not in class. |
|
|
|
|
POSIX class. |
|
|
|
|
Any char except newline. |
|
|
|
|
White space, |
|
|
|
|
Non-white space, |
|
|
|
|
Digit. |
|
|
|
|
Non-digit. |
|
|
|
|
Word, |
|
|
|
|
Non-word. |
|
|
|
Sequence#
Sequence |
Description |
Example |
Valid |
Invalid |
|---|---|---|---|---|
|
Alternation. |
|
|
|
|
Subpattern. |
|
|
|
|
Capture submatch into |
|
|
|
|
Subpattern, no capture. |
|
|
|
Quantifiers#
Quantifier |
Description |
Example |
Valid |
Invalid |
|---|---|---|---|---|
|
One or more. |
|
|
|
|
Zero or more. |
|
|
|
|
Zero or one. |
|
|
|
|
Zero or one, lazy. |
|
|
|
|
One or more, lazy. |
|
|
|
|
Zero or more, lazy. |
|
|
|
|
n times exactly. |
|
|
|
|
From n to m times. |
|
|
|
|
At least n times. |
|
|
|
|
If-then pattern. |
|
|
|
|
If-then-else pattern. |
|
|
|
Escapes#
Char |
Description |
|---|---|
|
General escape. |
|
New line. |
|
Carriage return. |
|
Tab. |
|
Vertical tab. |
|
Form feed. |
|
Alarm. |
|
Backspace. |
|
Escape. |
|
Ctrl + char ( |
|
Three-digit octal ( |
|
One or two-digit hexadecimal ( |
|
Any hexadecimal code ( |
|
Char with Unicode property ( |
|
Char without Unicode property. |
Unicode Properties#
\p{...} selects characters by Unicode property; \P{...}
is the negation. The most-used categories are below. Supported
by PCRE / PCRE2, Python re, JavaScript with the u flag
(ES2018+), Java, .NET, Ruby, Rust regex. Not supported
by Go regexp, POSIX BRE/ERE, or stock grep -P on some
GNU builds.
General categories#
Property |
Matches |
|---|---|
|
any letter |
|
lowercase letter |
|
uppercase letter |
|
titlecase letter |
|
other letter (no case, e.g. CJK) |
|
any combining mark |
|
non-spacing combining mark (accent) |
|
any numeric |
|
decimal digit |
|
letter-like number (Roman numerals) |
|
other numeric (fractions, superscript) |
|
any punctuation |
|
dash punctuation |
|
opening / closing punctuation |
|
initial / final quote punctuation |
|
any symbol |
|
currency symbol |
|
math symbol |
|
any separator |
|
space separator |
|
line / paragraph separator |
|
any “other” (control, surrogate, private use) |
|
control character |
|
format character (zero-width joiner, RTL marks) |
Scripts (common)#
Property |
Matches |
|---|---|
|
Latin script |
|
Greek script |
|
Cyrillic script |
|
Arabic script |
|
Hebrew script |
|
Han / CJK ideographs |
|
Japanese kana |
|
Korean Hangul |
|
Devanagari |
|
Thai script |
Operator picks#
Goal |
Pattern |
|---|---|
any letter, locale-independent |
|
alphanumeric (any script) |
|
strip emoji / pictographs |
|
detect bidi text injection |
|
punctuation-only line |
|
non-ASCII whitespace |
|
Modifiers#
Modifier |
Description |
|---|---|
|
Global match. |
|
Case-insensitive, match both upper and lower. |
|
Multiple lines. |
|
Single line (by default). |
|
Ignore whitespace, allow comments. |
|
Anchored, pattern is forced to |
|
Dollar end only, |
|
Extra analysis performed, useful for non-anchored patterns. |
|
Ungreedy, greedy patterns become lazy by default. |
|
Additional functionality of PCRE (PCRE extra). |
|
Allow duplicate names for subpatterns. |
|
Unicode, pattern and subject are treated as UTF-8. |
Backreferences#
Reuse a captured group in the same pattern (or in the replacement
text). PCRE / Python / Ruby / Perl use \1 … \9 and
named-group syntax; ECMAScript and Go’s regexp differ
slightly.
Syntax |
Description |
Example |
|---|---|---|
|
Backreference to numbered group. |
|
|
PCRE explicit numbered backref (avoids digit-collision). |
|
|
Python / PCRE named capture. |
|
|
ECMAScript / .NET / Ruby named capture. |
|
|
Python backref by name. |
|
|
PCRE / .NET / Ruby backref by name. |
|
Substitution#
The replacement-side syntax used by sed, awk, perl,
rg --replace, and many editors.
Token |
Description |
Example |
|---|---|---|
|
Backref to capture group N (engine-dependent). |
|
|
The entire match. |
|
|
Text after / before the match (Perl / JS). |
rare; mostly Perl one-liners. |
|
Uppercase / lowercase next char (Perl, vim). |
|
|
Uppercase / lowercase to |
|
|
End |
paired with |
Substitution by Engine#
Engine |
Replacement backref syntax |
|---|---|
PCRE / Perl |
|
Python |
|
sed (BRE) |
|
sed -E (ERE) |
|
JavaScript |
|
.NET |
|
Java |
|
Engines#
The major regex implementations the operator meets. Two families: backtracking (PCRE-flavored, feature-rich, ReDoS-prone) and finite-state (RE2-style, linear-time, no backrefs / lookarounds).
Engine |
Type |
Notes |
|---|---|---|
PCRE |
backtracking |
De-facto reference for “advanced” regex. Rich features; ReDoS-prone. |
PCRE2 |
backtracking + JIT |
Successor; includes JIT. Original PCRE deprecated. |
Perl |
backtracking |
The original “modern” regex. |
Python |
backtracking |
Stdlib; Unicode-conservative; no possessive / atomic by default. |
Python |
backtracking |
Third-party; closer to PCRE; named groups, possessive, Unicode properties. |
JavaScript |
backtracking |
ES2018+: lookbehind, named groups, |
Java |
backtracking |
Possessive + atomic; Unicode properties; lookbehind length constraint. |
.NET |
backtracking |
Rich features; balancing groups; possessive. |
Ruby |
Onigmo backtracking |
Similar to Java + Perl extensions. |
Rust |
finite-state (NFA / DFA) |
RE2-style; no backreferences, no lookarounds; guaranteed linear time. |
Go |
RE2-derived |
Like Rust; same trade-offs. |
RE2 |
NFA / DFA |
Google; safety-first; the model for Go / Rust. |
Hyperscan |
NFA / streaming |
Intel; high-throughput packet inspection. |
Feature Portability#
Which features port across engines. Y = supported, N =
not supported, ~ = supported with a flag or alternative
syntax.
Feature |
PCRE / Perl |
Python |
ECMAScript |
POSIX BRE |
Go |
|---|---|---|---|---|---|
|
Y |
Y |
Y |
N |
Y |
Lookahead |
Y |
Y |
Y |
N |
N |
Lookbehind |
Y |
Y (3.7+) |
Y (ES2018+) |
N |
N |
Named captures |
Y |
Y |
Y (ES2018+) |
N |
Y |
Backreferences |
Y |
Y |
Y |
|
N (RE2) |
Possessive quantifiers |
Y |
N |
N |
N |
N |
Atomic groups |
Y |
Y (3.11+) |
N |
N |
N |
Inline flags |
Y |
Y |
|
N |
Y |
Unicode properties |
Y |
Y |
Y ( |
N |
N |
Linear-time guarantee |
N |
N |
N |
N |
Y (RE2) |
POSIX Classes#
Portable name-based character classes. Use inside a bracket
expression: [[:digit:][:alpha:]_].
Class |
Matches |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
whitespace ( |
|
|
|
punctuation |
|
control characters |
|
printable ( |
|
visible ( |
|
|
Common Tasks#
The operator’s daily-driver invocations.
Goal |
Command |
|---|---|
extended regex with grep |
|
Perl-compatible regex with grep |
|
ripgrep (PCRE2 with |
|
case-insensitive search |
|
only the matched part |
|
count matches per file |
|
invert match |
|
recursive search |
|
search history-respecting ( |
|
sed substitution (BRE) |
|
sed substitution (ERE) |
|
sed substitution (PCRE) |
|
in-place edit |
|
awk match |
|
awk capture (gawk) |
|
perl one-liner (capture) |
|
find by regex name |
|
rename by regex |
|
URL-extract from text |
|
delete every blank line |
|
validate regex compiles |
|
Common Patterns#
Frequently-needed building blocks. For richer IOC patterns see Indicators of Compromise.
Match |
Pattern |
|---|---|
integer |
|
signed float |
|
hex literal |
|
leading / trailing whitespace |
|
blank line |
|
quoted string |
|
email (pragmatic) |
|
URL |
|
IPv4 |
|
MAC address |
|
UUID v4 |
|
SHA-256 |
|
ISO 8601 date |
|
CVE id |
|
IPv6 (uncompressed) |
|
Bitcoin address |
|
Ethereum address |
|
JWT |
|
Phone (NANP, US / Canada) |
|
US ZIP |
|
BIP-39 mnemonic (12 / 24 words) |
|
ReDoS Minefield (avoid)#
Patterns that explode on attacker-controlled input. Each runs in exponential time on a long sequence of the inner character.
Anti-pattern |
Why |
|---|---|
|
Nested |
|
Same structure with |
|
Nested |
|
Nested |
|
Overlap-based ambiguity: two ways to consume each |
|
Ambiguous boundary; quadratic on long inputs. |
Mitigations: possessive quantifiers (a++), atomic groups
((?>a+)), tighter character classes ([^x] instead of
.), or pick a DFA / RE2 engine (Go regexp, Rust
regex).
Pitfalls#
Pitfall |
Notes |
|---|---|
Greedy by default |
|
|
In most engines, |
|
|
Catastrophic backtracking |
Nested quantifiers like |
Word boundary surprises |
|
Anchored vs unanchored |
|
Locale and Unicode |
|
Engine flavors |
|
Linear-time vs backtracking |
Go |
HTML / XML with regex |
Don’t. Use a parser ( |
References#
regex101.com, interactive tester with explanations across PCRE / Python / JS / Go.
regexr.com, alternative tester.
RE2 syntax, Go’s engine.
Perl perlre, the reference PCRE-family grammar.
Indicators of Compromise, IOC regex catalog.
debuggex.com, regex railroad diagrams.