Catalog / Regular Expressions (Regex) Basics Cheatsheet
Regular Expressions (Regex) Basics Cheatsheet
A quick reference guide to the fundamental concepts and syntax of regular expressions, covering patterns, metacharacters, and common use cases.
Character Matching
Basic Characters
|
Matches the literal character. For example, |
|
Matches any single character except newline ( |
|
Matches any digit (0-9). |
|
Matches any word character (a-z, A-Z, 0-9, and underscore). |
|
Matches any whitespace character (space, tab, newline). |
|
Matches any non-digit character. |
|
Matches any non-word character. |
|
Matches any non-whitespace character. |
Character Sets
|
Matches any single character in the set (a, b, or c). |
|
Matches any single character not in the set (anything but a, b, or c). |
|
Matches any lowercase letter (a to z). |
|
Matches any digit (0 to 9). |
|
Matches any alphanumeric character or underscore (same as |
|
Matches a space character inside a character set. |
Quantifiers
Quantifier Basics
|
Matches the preceding character or group zero or more times. |
|
Matches the preceding character or group one or more times. |
|
Matches the preceding character or group zero or one time (optional). |
|
Matches the preceding character or group exactly n times. |
|
Matches the preceding character or group n or more times. |
|
Matches the preceding character or group between n and m times (inclusive). |
Greedy vs. Lazy Matching
Greedy |
By default, quantifiers are greedy, meaning they match the longest possible string. |
Lazy (Reluctant) |
Adding Example: |
Example |
Given the string |
Anchors and Grouping
Anchors
|
Matches the beginning of the string (or line, if multiline mode is enabled). |
|
Matches the end of the string (or line, if multiline mode is enabled). |
|
Matches a word boundary (the position between a word character and a non-word character). |
|
Matches a non-word boundary. |
Grouping and Capturing
|
Groups characters together and captures the matched group. Example: |
|
Backreferences to captured groups. Example: |
|
Non-capturing group. Groups characters together without capturing the matched group. Useful for applying quantifiers or alternations. Example: |
Alternation
|
Matches either the expression before or after the Example: |
Flags (Modes)
Common Flags
|
Case-insensitive matching. Matches both uppercase and lowercase letters. |
|
Global matching. Finds all matches rather than stopping after the first. |
|
Multiline mode. |
|
Dotall mode. Allows the dot ( |
|
Verbose mode. Allows whitespace and comments in the regex for better readability. |
Using Flags
Flags are often specified at the end of the regex pattern, e.g., |
In some languages, flags can be specified inline within the regex using the |