Add regex.md

This commit is contained in:
Zakary Timson 2023-12-11 21:50:40 +00:00
parent e36a32f52b
commit 91ebcf22af

112
regex.md Normal file
View File

@ -0,0 +1,112 @@
# R* Cheatsheet
## Modifiers
| Symbol | Description |
|--------|-------------------------------|
| `g` | Global - search entire string |
| `i` | Case-insensitive |
| `m` | Multiple lines |
| `s` | Treat as single line |
| `x` | Allow comments & whitespace |
| `e` | Evaluate placement |
| `U` | Un-greedy patterns by default |
## Escape
| Symbol | Description |
|--------|---------------------------|
| `?#` | Comment |
| `\ ` | Escape special characters |
| `\Q` | Begin literal sequence |
| `\E` | End literal sequence |
## Anchors
| Symbol | Description |
|--------|-------------------|
| `^` | Start of line |
| `$` | End of line |
| `\A` | Start of string |
| `\Z` | End of string |
| `\b` | Word boundary |
| `\B` | Not word boundary |
| `\<` | Start of word |
| `\>` | End of word |
## Characters
| Symbol | POSIX | Description |
|--------|--------------------------------------|------------------------------------------|
| `\c` | `[:cntrl:]` | Control character |
| `\S` | | Not a white space |
| `\s` | `[:blank:]` `[:space:]` | White space |
| | `[:punct:]` | Punctuation |
| `.` | `[:print:]` `[:graph:]` | Printed Character, with & without spaces |
| `\W` | | Not a letter |
| `\w` | `[:alpha:]` `[:upper:]` `[:lower:]` | Letter |
| `\D` | | Not a number |
| `\d` | `[:digit:]` | Number |
| `\x` | `[:xdigit:]` | Hexadecimal digit |
| `\O` | | Octal digit |
| `\n` | | New line |
| `\r` | | Carriage return |
| `\t` | | Tab |
| `\v` | | Vertical tab |
| `\f` | | Form feed |
| `\xxx` | | Octal number "xxx" |
| `\xaa` | | Hexidecimal "aa" |
## Repeat
| Symbol | Range | Description |
|----------|-------------|---------------------------------------|
| | `{x}` | Exactly `x` times |
| | `{min,max}` | Repeat between `min` & `max` times |
| `?` | `{,1}` | 0 or 1 |
| `*` `*?` | `{0,}` | 0 or many (Adding `?` make un-greedy) |
| `+` `+?` | `{1,}` | 1 or more (Adding `?` make un-greedy) |
Un-greedy repeaters will take the minimum number of characters required
## Group Selectors
| Symbol | Description |
|----------------|--------------------------------------------|
| `.` | Wildcard/Single character (Except newline) |
| `(abc\|123)` | `abc` or `123` |
| `(...)` | Capture group |
| `(?<name>...)` | Named capture group |
| `(?:...)` | Group un-captured |
| `[abc]` | `A` or `B` or `C` |
| `[^abc]` | Not `A` or `B` or `C` |
| `[0-9]` | Number between `0` and `9` |
| `[a-g]` | Lowercase letter between `a` and `g` |
| `[A-G]` | Uppercase letter between `A` and `G` |
All selectors are inclusive
## Reference Group
| Symbol | Description |
|---------|----------------|
| `$n` | N'th Group |
| `$\` ` | Before matched |
| `$'` | After matched |
| `$+` | Last match |
| `$&` | Entire match |
Some regex implementations use \ instead of $.
## Assertions
| Symbol | Description |
|--------------|-------------------------|
| `?=` | Lookahead |
| `?!` | Negative lookahead |
| `?<=` | Lookbehind |
| `?!=` `?<!` | Negative lookbehind |
| `?>` | Once-only subexpression |
| `?(...)` | If condition |
| `?(...) \| ` | If-else condition |