> For the complete documentation index, see [llms.txt](https://cp.rohanthe.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cp.rohanthe.dev/linux-essentials/regular-expressions-cheatsheet.md).

# Regular Expressions Cheatsheet

Basic Classes: Playing with characters

| Symbol  | Description                                                      | Example                                                                                                        |
| ------- | ---------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| `.`     | Matches any single character except newline.                     | `c.t` matches `cat`, `cut`, `cot`, etc.                                                                        |
| `*`     | Matches zero or more occurrences of the preceding character.     | `ab*c` matches `ac`, `abc`, `abbc`, `abbbc`, etc.                                                              |
| `+`     | Matches one or more occurrences of the preceding character.      | `ab+c` matches `abc`, `abbc`, `abbbc`, etc.                                                                    |
| `?`     | Matches zero or one occurrence of the preceding character.       | `colou?r` matches `color` or `colour`.                                                                         |
| `^`     | Matches the beginning of a line.                                 | `^hello` matches `hello` at the beginning of a line.                                                           |
| `$`     | Matches the end of a line.                                       | `world$` matches `world` at the end of a line.                                                                 |
| `[]`    | Matches any one of the enclosed characters.                      | `[abc]` matches `a`, `b`, or `c`.                                                                              |
| `[a-z]` | Matches any character between `a` and `z`.                       | `[a-z]+` matches any lowercase word.                                                                           |
| `[^]`   | Matches any character not enclosed in the brackets.              | `[^abc]` matches any character except `a`, `b`, or `c`.                                                        |
| `()`    | Groups multiple tokens together and creates a capture group.     | `([0-9]{3})-([0-9]{4})` matches a phone number in the format `123-4567` and captures the area code and number. |
| `\`     | Escapes a special character.                                     | `\.` matches a period.                                                                                         |
| `\d`    | Matches any digit character. Equivalent to `[0-9]`.              | `\d{3}` matches any three digits.                                                                              |
| `\w`    | Matches any word character. Equivalent to `[a-zA-Z0-9_]`.        | `\w+` matches any word.                                                                                        |
| `\s`    | Matches any whitespace character. Equivalent to `[ \t\n\r\f\v]`. | `\s+` matches any whitespace.                                                                                  |

Advanced Regex Concepts

| Concept                  | Symbol                                       | Explanation                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ------------------------ | -------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Quantifiers              | `{n}`\<br>`{n,m}`\<br>`*`\<br>`+`\<br>`?`    | Quantifiers specify how many times a pattern should match. `{n}` specifies an exact number of matches, `{n,m}` specifies a range of matches, `*` specifies zero or more matches, `+` specifies one or more matches, and `?` specifies zero or one match.                                                                                                                                                                                                                                                                                                                                                               |
| Alternation              | \`                                           | \`                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| Grouping                 | `()`                                         | Grouping allows you to create subexpressions within a regular expression. This can be useful for applying quantifiers or alternation to multiple characters at once. For example, `(abc)+` matches one or more repetitions of the sequence "abc".                                                                                                                                                                                                                                                                                                                                                                      |
| Backreferences           | `\1`, `\2`, etc.                             | Backreferences allow you to refer to a previously matched group within a regular expression. For example, `(\w+) \1` matches repeated words like "hello hello" or "cat cat".                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| Lookahead and Lookbehind | `(?=...)`, `(?!...)`, `(?<=...)`, `(?<!...)` | Lookahead and lookbehind are zero-width assertions that allow you to check for patterns before or after the current position in the string, without actually consuming any characters. Positive lookahead (`(?=...)`) matches if the pattern inside the lookahead matches, negative lookahead (`(?!...)`) matches if the pattern inside the lookahead does not match, positive lookbehind (`(?<=...)`) matches if the pattern inside the lookbehind matches behind the current position, and negative lookbehind (`(?<!...)`) matches if the pattern inside the lookbehind does not match behind the current position. |
