diff --git a/regex.md b/regex.md index c5467cd..65b0463 100644 --- a/regex.md +++ b/regex.md @@ -309,6 +309,171 @@ Lookaround lets you match a group before (lookbehind) or after (lookahead) your +Regex examples{.cols-3} +-------------- + +### Characters +| Pattern | Matches | +|---------------|-----------------------------------| +| `ring `| Match ring springboard etc. | +| `. `| Match a, 9, + etc. | +| `h.o `| Match hoo, h2o, h/o etc. | +| `ring\? `| Match ing? | +| `\(quiet\) `| Match quiet) | +| `c:\\windows `| Match c:\windows | + +Use `\` to search for these special characters:
`[ \ ^ $ . | ? * + ( ) { }` + + +### Alternatives + +| Pattern | Matches | +|----------------|------------------------| +| `cat|dog `| Match cat or dog | +| `id|identity `| Match id or identity | +| `identity|id `| Match id or identity | + +Order longer to shorter when alternatives overlap + +### Character classes +| Pattern | Matches | +|---------------|-----------------------------------| +| `[aeiou]` | Match any vowel | +| `[^aeiou]` | Match a NON vowel | +| `r[iau]ng` | Match ring, wrangle, sprung, etc. | +| `gr[ae]y` | Match gray or grey | +| `[a-zA-Z0-9]` | Match any letter or digit | + +In `[ ]` always escape `. \ ]` and sometimes `^ - .` + + +### Shorthand classes +| Pattern | Meaning | +|------------------|-------------------------------------------------| +| `\w ` | "Word" character
_(letter, digit, or underscore)_ | +| `\d ` | Digit | +| `\s ` | Whitespace
_(space, tab, vtab, newline)_ | +| `\W, \D, or \S ` | Not word, digit, or whitespace | +| `[\D\S] ` | Means not digit or whitespace, both match | +| `[^\d\s] ` | Disallow digit and whitespace | + + +### Occurrences +| Pattern | Matches | +|---------------------|-----------------------------------| +| `colou?r` | match color or colour | +| `[BW]ill[ieamy's]*` | match Bill, Willy, William's etc. | +| `[a-zA-Z]+` | match 1 or more letters | +| `\d{3}-\d{2}-\d{4}` | match a SSN | +| `[a-z]\w{1,7}` | match a UW NetID | + + + +### Greedy versus lazy +| Pattern | Meaning | +|---------------------------|----------------------------------| +| `* + {n,}`
_greedy_ | match as much as possible | +| `<.+> ` | finds 1 big match in \bold\<\/b> | +| `*? +? {n,}?`
_lazy_ | match as little as possible | +| `<.+?>` | finds 2 matches in \<b>bold\<\/b> | + + +### Scope {.col-span-2} +| Pattern | Meaning | +|--------------------|---------------------------------------------| +| `\b ` | "Word" edge (next to non "word" character) | +| `\bring ` | Word starts with "ring", ex ringtone | +| `ring\b ` | Word ends with "ring", ex spring | +| `\b9\b ` | Match single digit 9, not 19, 91, 99, etc.. | +| `\b[a-zA-Z]{6}\b ` | Match 6-letter words | +| `\B ` | Not word edge | +| `\Bring\B ` | Match springs and wringer | +| `^\d*$ ` | Entire string must be digits | +| `^[a-zA-Z]{4,20}$` | String must have 4-20 letters | +| `^[A-Z] ` | String must begin with capital letter | +| `[\.!?"')]$ ` | String must end with terminal puncutation | + + + +### Modifiers +| Pattern | Meaning | +|-------------------|--------------------------------------------------| +| `(?i)`[a-z]*`(?-i)` | Ignore case ON / OFF | +| `(?s)`.*`(?-s)` | Match multiple lines (causes . to match newline) | +| `(?m)`^.*;$`(?-m)` | ^ & $ match lines not whole string | +| `(?x)` | #free-spacing mode, this EOL comment ignored | +| `(?-x)` | free-spacing mode OFF | +| /regex/`ismx` | Modify mode for entire string | + + + +### Groups +| Pattern | Meaning | +|------------------|------------------------------| +| `(in\|out)put ` | Match input or output | +| `\d{5}(-\d{4})?` | US zip code _("+ 4" optional)_ | +Parser tries EACH alternative if match fails after group. +
+Can lead to catastrophic backtracking. + + + + +### Back references +| Pattern | Matches | +|--------------------------|--------------------------------------------------| +| `(to) (be) or not \1 \2` | match to be or not to be | +| `([^\s])\1{2}` | match non-space, then same twice more   aaa, ... | +| `\b(\w+)\s+\1\b` | match doubled words | + + + +### Non-capturing group +| Pattern | Meaning | +|---------------------|-----------------------------| +| `on(?:click\|load)` | Faster than:
`on(click\|load)` | + +Use non-capturing or atomic groups when possible + + + +### Atomic groups +| Pattern | Meaning | +|------------------------|----------------------------| +| `(?>red\|green\|blue)` | faster than non-capturing | +| `(?>id\|identity)\b` | match id, but not identity | + +"id" matches, but `\b` fails after atomic group, +parser doesn't backtrack into group to retry 'identity' +
+
+If alternatives overlap, order longer to shorter. + + + +### Lookaround {.row-span-2 .col-span-2} +| Pattern | Meaning | +|-------------------------|--------------------------------------| +| `(?= )` | Lookahead, if you can find ahead | +| `(?! )` | Lookahead,if you can not find ahead | +| `(?<= )` | Lookbehind, if you can find behind | +| `(?warbling, string, fishing, ... | +| `\b(?!\w+ing\b)\w+\b` | Words NOT ending in "ing" | +| `(?<=\bpre).*?\b ` | Match pretend, present, prefix, ... | +| `\b\w{3}(?