The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
A regular expressions cheat sheet is a quick reference for patterns that search, extract, validate, split, or replace text. The portable core includes literals, character classes, anchors, quantifiers, groups, alternation, and lookarounds, but JavaScript, Python, PCRE2, .NET, and Java differ in flags, Unicode behavior, APIs, and advanced syntax.
Use the tables below as a flavor-aware reference: identify the regex engine first, copy the pattern second, and test it in the same runtime that will execute it. A pattern that works in JavaScript or a regex tester may require different escaping or unsupported-feature changes in Python, Java, .NET, Go, Rust, or a command-line tool.
Key takeaways
- Regular expressions describe text patterns for searching, matching, extracting, replacing, and splitting, but regex syntax is not completely universal.
- The most portable building blocks are literals, character classes,
.,^,$, quantifiers, groups, noncapturing groups, and alternation. d,w,s, andbvary by flavor and Unicode mode; do not assume they mean the same thing in every engine.- Use a full-match API such as Python’s
re.fullmatch()when an entire string must be validated instead of relying only on^...$. - Regex can check the shape of dates, URLs, email addresses, and numbers, but parsers and application logic should handle semantic validation.
- Backtracking patterns with nested ambiguous repetition can create denial-of-service risks; use input limits, timeouts, safer engines, or atomicity where appropriate.
What is a regular expression?
A regular expression, often shortened to regex or regexp, is a pattern interpreted by a regex engine. The engine compares the pattern with input text and can return a yes-or-no result, a substring, multiple matches, captured groups, or replacement output.
Recommended Free Tools
For example, the pattern cat matches the literal sequence cat. The pattern c.t usually matches cat, cot, and cut because . represents one character. The precise behavior of ., character classes, flags, Unicode, and advanced constructs depends on the regex flavor.
Popular on Amazon
Regex is well suited to many flat text tasks, such as finding identifiers, extracting numbers, splitting records, or checking a basic input shape. A parser is normally a better choice for nested or recursive formats such as arbitrary HTML, XML, JSON, programming languages, and complex locale-aware dates or numbers.
How should you read regex notation?
A regex pattern is not always written in the same notation as the program that contains it. A programming-language string parser may process backslashes before the regex engine receives the pattern.
| Context | Example | Meaning |
|---|---|---|
| Regex pattern notation | d+ |
One or more digits, subject to the engine’s definition of d. |
| JavaScript literal | /d+/ |
A regex literal with no flags. |
| Python raw string | r"d+" |
A Python string that preserves the backslash for the regex engine. |
| Python ordinary string | "\d+" |
The backslash is doubled for Python’s string parser. |
| Java string | "\d+" |
Java source escaping produces the regex d+. |
| C# verbatim string | @"d+" |
A C# representation that avoids doubling this backslash. |
Always check both layers: the host-language string syntax and the regex syntax. Python’s regular-expression documentation specifically distinguishes regex escapes from string-literal behavior. For example, b outside a character class means a word boundary to the regex engine, while an ordinary Python string can interpret b as a backspace.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsQuick regular expressions cheat sheet
This regular expressions cheat sheet lists the syntax most readers use first. The entries are broadly portable, but Unicode behavior, newline handling, API behavior, and advanced extensions still vary.
Which literal characters and escapes matter?
| Syntax | Meaning | Example |
|---|---|---|
abc |
Literal text | cat matches cat. |
|
Escapes a metacharacter or starts a special sequence | . matches a literal period. |
\ |
Matches a literal backslash in many flavors | \ |
Q...E |
Quotes text literally in flavors that support it | Common in Java and some PCRE-style engines; not portable. |
Characters that commonly need escaping include . ^ $ * + ? ( ) [ ] { } | . Escaping rules also differ inside and outside character classes. A literal hyphen, closing bracket, or caret may require escaping or careful placement inside [...].
How do character classes work?
| Syntax | Meaning |
|---|---|
[abc] |
One character: a, b, or c. |
[^abc] |
One character other than a, b, or c. |
[a-z] |
One character in the ASCII range from a through z. |
[A-Z] |
One uppercase ASCII letter. |
[0-9] |
One ASCII digit. |
[a-zA-Z0-9_] |
A common ASCII approximation of a word character. |
[.] |
A literal period. |
[abc&&[^b]] |
Class intersection in flavors that support it; not portable. |
Do not describe [a-z] as “any letter.” The range usually means ASCII lowercase letters, not every alphabetic character in Unicode. For example, [0-9A-Fa-f]{2} matches two hexadecimal ASCII characters, while [^,s]+ matches one or more characters that are neither commas nor whitespace.
What do d, w, s, and . mean?
| Syntax | Common meaning | Important qualification |
|---|---|---|
d |
A digit | May mean ASCII digits or Unicode decimal digits. |
D |
Not a digit | Inherits the flavor’s digit definition. |
w |
A word character | Often includes letters, digits, and underscore; Unicode scope differs. |
W |
Not a word character | Depends on the same flavor-specific word definition. |
s |
Whitespace | The exact set of whitespace characters differs. |
S |
Not whitespace | Inherits the flavor’s whitespace definition. |
. |
Usually any character except line terminators | Dotall or singleline mode can make it match line terminators. |
Python string patterns use Unicode matching by default, while Python’s ASCII flag can restrict certain classes and boundary behavior; Python bytes patterns behave differently. JavaScript has its own w and Unicode rules. Consult the MDN JavaScript regex cheat sheet and Python re documentation before treating these shorthands as interchangeable.
Where supported, Unicode property escapes are more explicit:
p{L} # a Unicode letter
p{Letter} # long-form Unicode letter property
p{Script=Greek} # a character in the Greek script
P{L} # a character that is not a Unicode letter
JavaScript documents Unicode property escapes and requires Unicode-aware regular-expression syntax and the appropriate flags. Unicode properties are not available in every flavor, and supported property names can differ.
Which anchors and boundaries should you use?
| Syntax | Meaning |
|---|---|
^ |
Start of input, or start of a line in multiline mode. |
$ |
End of input, or end of a line in multiline mode; trailing-newline behavior varies. |
A |
Absolute start of input in flavors that support it. |
Z |
End-of-input anchor with flavor-specific newline behavior. |
z |
Strict absolute end in flavors that support it. |
b |
A transition between word and non-word characters. |
B |
A position that is not a word boundary. |
G |
The previous match position in flavors that support it. |
^cat$ matches an input consisting only of cat in a simple single-line case. bcatb matches cat as a whole engine-defined word, but not the cat inside scatter.
For full-string validation, prefer a full-match API where available. Python’s re.fullmatch() is clearer than relying only on anchors. The .NET regex behavior documentation also warns that matching APIs such as Regex.Match can return a substring unless the pattern or API is designed for full-string matching.
How do quantifiers control repetition?
| Syntax | Meaning |
|---|---|
* |
Zero or more. |
+ |
One or more. |
? |
Zero or one. |
{n} |
Exactly n. |
{n,} |
At least n. |
{n,m} |
Between n and m. |
*? +? ?? {n,m}? |
Lazy versions that initially consume as little as possible. |
++ or (?>...) |
Possessive repetition or an atomic group in supporting flavors. |
Greedy quantifiers try to consume as much as possible. Lazy quantifiers try to consume as little as possible, but lazy does not mean safe. Nested or ambiguous repetition can still cause excessive backtracking. PCRE2 documents possessive quantifiers and atomic groups as engine features; JavaScript does not provide every PCRE2 extension.
Also popular on Amazon
How do alternation and grouping work?
| Syntax | Meaning |
|---|---|
a|b |
Match a or b. |
(abc) |
Capturing group. |
(?:abc) |
Noncapturing group. |
(cat|dog) |
Captures either cat or dog. |
Alternation has lower precedence than concatenation. Therefore, cat|dog means “match cat or dog,” while gr(a|e)y means gray or grey. The pattern ^cat|dog$ does not generally require the entire input to be either word. Use ^(?:cat|dog)$ when the complete input must be one of the two choices.
Capturing groups store text for later inspection or replacement. Use (?:...) when grouping is structural only. Group numbers are assigned by opening parenthesis from left to right, so adding an early capturing group can change every later numeric backreference and replacement reference.
How do backreferences work?
| Syntax | Meaning |
|---|---|
1 |
Backreference to capture group 1. |
k<name> |
Named backreference in several flavors. |
(?P=name) |
Python named backreference. |
(?<name>abc) |
Named group syntax in .NET and several other flavors. |
(?P<name>abc) |
Python-style named group. |
b(w+)s+1b can find duplicated words such as the the, subject to the engine’s definition of w. Named-group syntax is not universal, so check the target flavor before copying a named pattern.
Free tools Windows power users keep installed
One-click scans. No signup required.
What are lookarounds and assertions?
Assertions test a position without consuming the asserted text. JavaScript’s MDN assertions reference covers boundaries, lookahead, lookbehind, and related constructs.
| Syntax | Meaning | Example |
|---|---|---|
(?=...) |
Positive lookahead | d+(?= dollars) matches digits followed by dollars. |
(?!...) |
Negative lookahead | ^(?!.*badminb).+$ rejects input containing admin as an engine-defined whole word. |
(?<=...) |
Positive lookbehind | (?<=$)d+ matches digits preceded by $ where supported. |
(?<!...) |
Negative lookbehind | Checks that the current position is not preceded by the asserted text. |
Lookbehind support and restrictions vary. Some engines require fixed-length lookbehind, some support more flexible alternatives, and older runtimes may not support it at all. Python documents lookaround restrictions in its re reference.
Which regex flags are common?
| Flag | Common meaning | Portability note |
|---|---|---|
i |
Case-insensitive matching. | Unicode case folding differs. |
m |
Multiline anchors. | Changes the meaning of ^ and $. |
s |
Dotall: dot matches line terminators. | Also called singleline in some APIs. |
g |
Global or repeated matching in JavaScript. | Not a universal regex flag. |
u |
Unicode-aware mode in JavaScript. | JavaScript-specific behavior. |
v |
Newer JavaScript Unicode-aware mode with additional character-set capabilities. | Runtime support is version-dependent. |
y |
Sticky JavaScript matching at lastIndex. |
JavaScript-specific. |
d |
Match indices in JavaScript. | JavaScript-specific. |
x |
Free-spacing and comments mode in many non-JavaScript flavors. | Whitespace and comment rules vary. |
U, a, A |
Flavor-specific ungreedy, ASCII, or anchoring modes. | Never assume the same meaning across engines. |
Python exposes flags such as re.IGNORECASE, re.MULTILINE, re.DOTALL, re.VERBOSE, and re.ASCII. Python’s Unicode flag is redundant for Unicode str patterns and remains mainly for backward compatibility.
What are the most useful regex examples?
The following patterns are practical starting points, not universal validators. Replace ASCII shorthands with explicit Unicode properties or application-specific logic when internationalized text matters.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minute| Task | Pattern | What it does and does not guarantee |
|---|---|---|
| One or more digits | d+ |
Finds consecutive engine-defined digits. |
| Integer | [+-]?d+ |
Allows an optional sign and one or more digits. |
| Decimal with exponent | [+-]?(?:d+(?:.d*)?|.d+)(?:[eE][+-]?d+)? |
Checks a simple dot-decimal structure, not locale-specific numeric syntax. |
| Whitespace run | s+ |
Finds one or more engine-defined whitespace characters. |
| Whole word | bwordb |
Uses the flavor’s word-character rules, not a universal natural-language boundary. |
| US ZIP code shape | ^d{5}(?:-d{4})?$ |
Accepts five digits with an optional hyphen and four digits; it does not prove assignment. |
| Basic email shape | ^[^@s]+@[^@s]+.[^@s]+$ |
Useful for a basic UI check, not complete email-standard validation or delivery verification. |
| Basic HTTP(S) shape | ^https?://[^s]+$ |
A lightweight filter only; use a URL parser and scheme/host security checks. |
| Text in square brackets | [([^]]*)] |
Captures text up to the next closing bracket; it does not handle arbitrary nesting. |
| Splitting on comma and optional surrounding whitespace | s*,s* |
Useful as a separator in a split API; quoted commas require a CSV parser. |
How can you validate integers and decimals?
[+-]?d+
The integer pattern permits an optional plus or minus sign. A simple decimal pattern is:
[+-]?(?:d+(?:.d*)?|.d+)
Adding an optional exponent gives:
[+-]?(?:d+(?:.d*)?|.d+)(?:[eE][+-]?d+)?
These expressions do not handle every locale’s decimal separator, thousands separator, currency symbol, or accounting notation. Use a locale-aware numeric parser after any initial structural check.
How can you check an ISO-like date?
^d{4}-d{2}-d{2}$
This checks the shape YYYY-MM-DD, not whether the date exists. A stricter structural pattern restricts the month and day ranges:
^(?:d{4})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]d|3[01])$
The stricter pattern can still accept February 31 and does not calculate leap years. Use a date parser for semantic validation.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →How should you match email addresses and URLs?
Use ^[^@s]+@[^@s]+.[^@s]+$ only as a basic email-shape check. When ownership matters, send a verification message; a regex cannot prove that an address is deliverable or controlled by the user.
Also popular on Amazon
- This 4-page 8.5" x 11" laminated medical chart quick reference Guide is the ultimate reference for the Muscular System!
- This chart contains full-color illustrations, as well as different views and layers, of muscles in the head, torso, and extremities.
Do not present one giant URL regex as universally correct. URL processing includes schemes, internationalized domains, ports, paths, queries, fragments, credentials, normalization, and security policy. Prefer a URL parser, allow only permitted schemes such as https when appropriate, and validate hosts according to the application.
How can you match quoted strings?
For a simple non-newline quoted string with no escapes, use:
"[^"rn]*"
For a quoted string that permits backslash escapes, use:
"(?:\.|[^"\rn])*"
Both patterns are application-dependent. Use a JSON parser for JSON and a lexer or parser for a programming language instead of treating a regex as a complete grammar.
How do you extract text between delimiters?
[([^]]*)] captures text inside square brackets without crossing the next closing bracket. A lazy pattern such as [(.*?)] may also work for a simple case, but a constrained character class makes the intended boundary clearer.
Nested brackets require recursive or stack-based processing in many applications. PCRE2 and some Perl-compatible engines provide recursion or subroutine features, documented in the PCRE2 pattern reference, but those features are not portable.
How do replacement tokens work?
Replacement syntax varies more than pattern syntax. The replacement string is interpreted by the host API, so do not copy a JavaScript replacement directly into Python, .NET, or Java.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
| Replacement concept | Common forms | Warning |
|---|---|---|
| Entire match | $&, |