Catalog / Regular Expressions Cheatsheet
Regular Expressions Cheatsheet
A quick reference guide to regular expressions (regex) in programming, covering syntax, common patterns, and usage examples.
Regex Basics & Metacharacters
Basic Matching
|
Matches the literal character sequence. Example: |
|
Matches any single character except newline. Example: |
|
Matches the beginning of the string. Example: |
|
Matches the end of the string. Example: |
|
Character class: Matches any single character within the brackets. Example: |
|
Negated character class: Matches any single character not within the brackets. Example: |
|
Alternation: Matches either the expression before or after the |
Quantifiers
|
Matches the preceding character or group zero or more times. Example: |
|
Matches the preceding character or group one or more times. Example: |
|
Matches the preceding character or group zero or one time. Example: |
|
Matches the preceding character or group exactly |
|
Matches the preceding character or group |
|
Matches the preceding character or group between |
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. |
Grouping and Backreferences
Grouping
|
Groups the enclosed pattern. Allows you to apply quantifiers or alternations to the entire group. Also captures the matched group for backreferencing. |
|
Non-capturing group. Groups the pattern but does not capture the matched group. Useful for performance or when you don’t need the captured value. |
Backreferences
|
Refers to the first, second, etc. captured group in the regex. Example: |
|
Refers to the first, second, etc. captured group in the replacement string of a substitution operation. |
Examples
Match a date in
|
Match an email address (simplified):
|
Match HTML tags:
|
Anchors and Lookarounds
Anchors
|
Matches the beginning of the string (or line, in multiline mode). |
|
Matches the end of the string (or line, in multiline mode). |
|
Matches a word boundary (the position between a word character and a non-word character). |
|
Matches a non-word boundary. |
Lookarounds
|
Positive lookahead: Asserts that the pattern follows the current position, but does not consume the characters. Example: |
|
Negative lookahead: Asserts that the pattern does not follow the current position. Example: |
|
Positive lookbehind: Asserts that the pattern precedes the current position, but does not consume the characters. Example: |
|
Negative lookbehind: Asserts that the pattern does not precede the current position. Example: |
Flags/Modifiers
Common Flags
|
Case-insensitive matching. Example: |
|
Global matching. Finds all matches instead of stopping after the first. |
|
Multiline mode. |
|
Dotall mode. Allows the |
|
Verbose mode. Allows whitespace and comments in the regex pattern for better readability. Whitespace is ignored, and comments start with |
Using Flags (Examples)
In Python:
|
In JavaScript:
|
In Ruby:
|