tags: #vim #vi #regex #regularexpression ## Characters ```plaintext . -- any character [abc] -- any character in {a,b,c} [a-z] -- any character from a-z [A-Z] [0-9] -- other ranges for character classes [^abc] -- any character other than {a,b,c} \l -- lowercase letter \L -- non lowercase letter (negation of \l) \x -- hex digit \X -- not hex digit \s -- whitespace \S -- not whitespace \w -- word (lower, upper alphabet, numbers and underscore) \W -- not word \d -- numeric digit (i.e. [0-9] \D -- not numeric digit (anything but 0-9) \o -- octal digit \O -- non octal digit \p -- printable character \P -- like \p but excluding digits \u -- uppercase character \U -- non uppercase character \a -- alphabetic character \A -- non alphabetic character ``` ## Quantifiers ```plaintext x\{3} -- match xxx x\{3,5} -- match xxx,xxxx,xxxxx {n} -- exactly n {n,m} -- from n to m inclusive {n,} -- at least n {,m} -- at most m {} -- zero or more (same as *) * -- zero or more + -- one or more ? -- zero or one ``` By default, quantified matches are **greedy** (i.e. match as much as possible). For **lazy** (i.e. match as little as possible) ```plaintext {-n,m} {-n,} {-,m} {-n} -- basically put {- instead of { ``` ## Anchors ```plaintext ^ -- start of line $ -- end of line \ -- word ending with nut ^ $ \< \> -- are known as zero-width ``` ## Alternatives ```plaintext hello|world -- match hello or world ``` ## Groups ```plaintext \(hello\) -- capture subexpression -- use \1 \2 etc. to refer to subexpressions in replacement string \0 -- in replacement string, the whole match & -- does the same as \0 \%(hello\) -- non capture group \%(hello\)\{2,3} -- match hellohello or hellohellohello ``` ## Look-arounds ```plaintext foo\zebar -- match "foo" provided it is followed by "bar" foo\%(bar\)\@= -- does the same foo\zsbar -- match "bar" provided it is preceded by "foo" \%(foo\)\@<=bar -- does the same -- use ! instead of = for negative lookahead and lookbehind ``` ## More ```plaintext do%[nut] -- matches do don donu donut chocolate\_sdonut -- matches chocolate followed by whitespace followed by donut, including newlines \%^ \%$ -- start and end of file \%V -- start within visual area ```