> For the complete documentation index, see [llms.txt](https://notes.qazeer.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.qazeer.io/miscellaneous/regex101.md).

# Regex 101

### Online resources

Online resources for regular expressions:

| Website                 | Description                                                                                                                             |
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| <https://regex101.com/> | <p>Online testing of regex, supporting different regex types / languages.<br><br>Detailed explanation of the different regex parts.</p> |

### Regex basic cheatsheet

| keyword                                                    | Description                                                                         |
| ---------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| `a`                                                        | Matches the character a.                                                            |
| `a\|b`                                                     | Matches either the character a or b.                                                |
| `a?`                                                       | Matches zero or one a.                                                              |
| `a*`                                                       | Matches zero or more a.                                                             |
| `a+`                                                       | Matches one or more a, as many times as possible, giving back as needed (greedy).   |
| `a+?`                                                      | Matches one or more a, as few times as possible, expanding as needed (lazy).        |
| `a{5}`                                                     | Matches exactly 5 consecutive a.                                                    |
| `a{5-10}`                                                  | Matches between 5 to 10 (inclusive) consecutive a.                                  |
| `a{5,}`                                                    | Matches 5 or more consecutive a.                                                    |
| `.`                                                        | Matches any single character.                                                       |
| `\s`                                                       | Matches any single space, tab or newline character.                                 |
| `\S`                                                       | Matches any single character that is not a space, tab or newline.                   |
| <p><code>\d</code><br><br><code>\[0-9]</code></p>          | Matches any single digit character.                                                 |
| <p><code>\D</code><br><br><code>\[^0-9]</code></p>         | Matches any single character that is not a digit.                                   |
| <p><code>\w</code><br><br><code>\[a-zA-Z0-9\_]</code></p>  | Matches any single letter, digit, or underscore.                                    |
| <p><code>\W</code><br><br><code>\[^a-zA-Z0-9\_]</code></p> | Matches any single any single character that is not a letter, digit, or underscore. |
| `^`                                                        | Matches the start of the string.                                                    |
| `^abc`                                                     | Matches a string starting by abc.                                                   |
| `$`                                                        | Matches the end of the string.                                                      |
| `xyz$`                                                     | Matches a string ending by xyz.                                                     |
| `[xyz]`                                                    | Matches a single character among x, y, or z.                                        |
| `[^xyz]`                                                   | Matches any character except for x, y or z.                                         |
| `[a-z]`                                                    | Matches a single character in the range a to z.                                     |
| `[a-zA-Z]`                                                 | Matches a single character in the range a to z or A to Z.                           |
| `[^a-z]`                                                   | Matches any character except those in the a to z range.                             |

### Regex examples

| Regex                                                                                                  | Description                                            |
| ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------ |
| `((25[0-5]\|2[0-4][0-9]\|1?[0-9][0-9]?)\\.){3}(25[0-5]\|2[0-4][0-9]\|[01]?[0-9][0-9]?)(:[1-65535]\D)?` | Matches an IPv4 address, with an eventual port number. |
