2.5 KiB
2.5 KiB
Regular Expressions (Regex) Cheat Sheet
In this cheat sheet, we'll explore common Regular Expressions (regex) patterns and their meanings.
Table of Contents
- Anchors
- Character Classes
- Quantifiers
- Grouping and Capturing
- Alternation
- Assertions
- Modifiers
- Escape Characters
- Special Characters
Anchors
^: Asserts the start of a line.$: Asserts the end of a line.\b: Asserts a word boundary.\B: Asserts a non-word boundary.
Character Classes
.: Matches any single character except a newline.[abc]: Matches any one of the characters a, b, or c.[^abc]: Matches any character except a, b, or c.[a-z]: Matches any lowercase letter.[A-Z]: Matches any uppercase letter.[0-9]: Matches any digit.\d: Matches any digit (short for[0-9]).\D: Matches any non-digit.\w: Matches any word character (alphanumeric + underscore).\W: Matches any non-word character.\s: Matches any whitespace character.\S: Matches any non-whitespace character.
Quantifiers
*: Matches 0 or more occurrences of the preceding character or group.+: Matches 1 or more occurrences of the preceding character or group.?: Matches 0 or 1 occurrence of the preceding character or group.{n}: Matches exactly n occurrences of the preceding character or group.{n,}: Matches n or more occurrences of the preceding character or group.{n,m}: Matches between n and m occurrences of the preceding character or group.
Grouping and Capturing
(...): Groups patterns together. Captures the matched text.(?:...): Groups patterns together without capturing.\1,\2, ...: Refers to the first, second, etc. captured group.
Alternation
|: Acts like a logical OR. Matches the pattern before or after the pipe.
Assertions
(?=...): Positive lookahead assertion.(?!...): Negative lookahead assertion.(?<=...): Positive lookbehind assertion.(?<!...): Negative lookbehind assertion.
Modifiers
i: Case-insensitive matching.g: Global matching (find all matches).m: Multiline matching (^and$match the start/end of each line).
Escape Characters
\: Escapes a special character, allowing it to be treated as a literal.
Special Characters
*,+,?,{,},|,(),[],.,\: Special characters with their literal meanings when escaped.