RegEx Cheatsheet
RegEx Basics
- a - match the character 'a'. This works for all non-special characters
- abc - match the phrase 'abc'. This works for all non-special characters
- . - match any character except a newline
- | - OR (a|b would mean match 'a' OR 'b')
- * - match 0 or more in a row (a* would match 'aaaa')
- ? - match zero or one time
- + - match one or more times
- \ - escape a special character
- () - group a set of characters together and remember the match
- [] - a range of characters ([a-zA-Z] matches all lower and uppercase letters)
- {} - matches a specified number of times (a{3} matches 'aaa')
- ^ - the beginning of a string
- $ - the end of a string
Characters
- \d - a digit (0-9)
- \D - a character that is not a digit
- \w - a word character
- \W - a non-word character
- \s - a whitespace character
- \S - a non-whitespace character
- \n - a new line
- \r - a carriage return
- \t - a tab character
- \b - a word boundary
- \B - not a word boundary
Logic
- | - OR
- () - capture the grouping between the parens
- (abc) - a capture of 'abc'
- \1 - the capture from group 1
- \2 - the capture from group 2
- (?:) - non-captured group
- (?:abc) - matches 'abc' but does not remeber the match
Brackets
- [abc] - matches 'a' or 'b' or 'c'
- [a-z] - matches and lowercase letter (from 'a' to 'z')
- [^abc] - matches anything other than 'a', 'b' or 'c'
- [^a] - matches anything but 'a'
- [\d\D] - Any character, including new lines (a dot does not match newlines)
- [\d\D]* - 0 or more characters. All characters are matched
Exiting
- :w - write (save) the file, but don't exit
- :wq or :x or ZZ - write (save) and quit
- :q - quit (fails if there are unsaved changes)
- :q! or ZQ - quit and throw away unsaved changes