Catalog / Regular Expressions Cheat Sheet
Regular Expressions Cheat Sheet
A quick reference guide to regular expressions, covering syntax, metacharacters, common patterns, and usage examples for text manipulation.
Regex Fundamentals
Basic Metacharacters
|
Matches any single character except newline. Example: |
|
Matches the beginning of the string. Example: |
|
Matches the end of the string. Example: |
|
Defines a character class, matching any character within the brackets. Example: |
|
Matches any character not within the brackets. Example: |
|
Acts as an “OR” operator, matching either the expression before or after the pipe. Example: |
|
Groups parts of the regex together and captures the matched substring. Example: |
Quantifiers
|
Matches the preceding character zero or more times. Example: |
|
Matches the preceding character one or more times. Example: |
|
Matches the preceding character zero or one time. Example: |
|
Matches the preceding character exactly Example: |
|
Matches the preceding character Example: |
|
Matches the preceding character between Example: |
Character Classes & Anchors
Predefined Character Classes
|
Matches any digit (0-9). Equivalent to |
|
Matches any non-digit character. Equivalent to |
|
Matches any word character (alphanumeric and underscore). Equivalent to |
|
Matches any non-word character. Equivalent to |
|
Matches any whitespace character (space, tab, newline, etc.). |
|
Matches any non-whitespace character. |
Anchors
|
Matches the beginning of a line. Example: |
|
Matches the end of a line. Example: |
|
Matches a word boundary (the position between a word character and a non-word character). Example: |
|
Matches a non-word boundary. Example: |
Groups and Lookarounds
Capturing Groups
(…) |
Captures the matched portion of the string. The captured group can be referenced later (e.g., in backreferences or when extracting matches). Example: |
|
Backreferences to previously captured groups. Example: |
Non-Capturing Groups
(?:…) |
Groups the pattern but does not capture the matched substring. Useful for grouping without the overhead of capturing. Example: |
Lookarounds (Zero-Width Assertions)
(?=…) |
Positive lookahead. Asserts that the pattern is followed by the given subpattern, without consuming the subpattern. Example: |
(?!…) |
Negative lookahead. Asserts that the pattern is not followed by the given subpattern. Example: |
(?<=…) |
Positive lookbehind. Asserts that the pattern is preceded by the given subpattern, without consuming the subpattern. Note: Not supported in all regex engines, and lookbehind assertions often have length restrictions. Example: |
(?<!…) |
Negative lookbehind. Asserts that the pattern is not preceded by the given subpattern. Note: Not supported in all regex engines, and lookbehind assertions often have length restrictions. Example: |
Flags & Common Patterns
Common Flags (Modifiers)
|
Case-insensitive matching. Example: |
|
Global matching. Finds all matches rather than stopping after the first. Example: |
|
Multiline mode. Example: |
|
Dotall mode. The Example: |
|
Verbose mode. Allows whitespace and comments within the regex pattern (for better readability). Whitespace is ignored unless escaped or within a character class. Comments start with Example:
|
Common Regex Patterns
Matching an email address: |
Matching a URL: |
Matching an IP address: |
Matching a date (YYYY-MM-DD): |
Matching a US phone number: |
Matching HTML tags: |