80 lines
2.5 KiB
Markdown
80 lines
2.5 KiB
Markdown
# Regular Expressions (Regex) Cheat Sheet
|
|
|
|
In this cheat sheet, we'll explore common Regular Expressions (regex) patterns and their meanings.
|
|
|
|
## Table of Contents
|
|
|
|
1. [Anchors](#anchors)
|
|
2. [Character Classes](#character-classes)
|
|
3. [Quantifiers](#quantifiers)
|
|
4. [Grouping and Capturing](#grouping-and-capturing)
|
|
5. [Alternation](#alternation)
|
|
6. [Assertions](#assertions)
|
|
7. [Modifiers](#modifiers)
|
|
8. [Escape Characters](#escape-characters)
|
|
9. [Special 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.
|
|
|
|
|