Catalog / Regular Expressions Cheat Sheet
Regular Expressions Cheat Sheet
A concise reference for regular expressions (regex) syntax and usage, covering patterns, metacharacters, quantifiers, and common operations.
Regex Fundamentals
Basic Patterns
|
Matches the literal sequence |
|
Matches any single character: |
|
Matches any single character except |
|
Matches any lowercase letter from |
|
Matches any digit from |
|
Matches any single character (except newline). |
Metacharacters
|
Matches any digit (same as |
|
Matches any non-digit character (same as |
|
Matches any word character (alphanumeric and underscore, same as |
|
Matches any non-word character (same as |
|
Matches any whitespace character (space, tab, newline). |
|
Matches any non-whitespace character. |
Anchors
|
Matches the beginning of the string. |
|
Matches the end of the string. |
|
Matches a word boundary (the position between a word character and a non-word character). |
|
Matches a non-word boundary. |
Quantifiers and Grouping
Quantifiers
|
Matches the preceding element 0 or more times. |
|
Matches the preceding element 1 or more times. |
|
Matches the preceding element 0 or 1 time. |
|
Matches the preceding element exactly n times. |
|
Matches the preceding element n or more times. |
|
Matches the preceding element between n and m times (inclusive). |
Grouping and Capturing
|
Groups the enclosed pattern. Captures the matched text for backreferencing. |
(?:pattern) |
Non-capturing group. Groups the pattern without capturing the matched text. |
|
Acts as an ‘or’ operator. Matches either the pattern before or after the |
|
Named capturing group. Matches |
|
Backreferences to the captured groups. |
Greedy vs. Lazy Matching
By default, quantifiers are greedy, meaning they match as much as possible. Add a Example: Given the string
|
Advanced Regex Features
Lookarounds
|
Positive lookahead assertion. Ensures that the pattern is followed by |
|
Negative lookahead assertion. Ensures that the pattern is not followed by |
|
Positive lookbehind assertion. Ensures that the pattern is preceded by |
|
Negative lookbehind assertion. Ensures that the pattern is not preceded by |
Flags/Modifiers
|
Case-insensitive matching. |
|
Global matching (find all matches, not just the first). |
|
Multiline matching. |
|
Dotall. Allows |
Conditional Regex
|
Common Regex Operations
Substitution
Replace matches of a pattern with a specified string. Example (Python):
|
Splitting
Split a string into a list of substrings based on a regex delimiter. Example (JavaScript):
|
Validation
Verify that a string matches a specific format using regex. Example (Java):
|