Skip to content

Instantly share code, notes, and snippets.

@hoang-tran
Last active July 29, 2016 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoang-tran/ee4e0e0285473d3051d4a7f2f3175e4f to your computer and use it in GitHub Desktop.
Save hoang-tran/ee4e0e0285473d3051d4a7f2f3175e4f to your computer and use it in GitHub Desktop.

Match

. : match any single character
* : match 0 or more times
\+ : match 1 or more times
\= : match 0 or 1 time (match optional)
\( : open group
\) : close group

Count

\{,m} : match at most m times
\{n,} : match at least n times
\{n,m} : match from n to m times inclusively
\{n} : match exactly n times

Match as little as possible

\{-} : match 0 or more times, as little as possible
\{-,m} : match at most m times, as little as possible
\{-n,} : match at least n times, as little as possible
\{-n,m} : match from n to m times, as little as possible
\{-n} : match exactly n times, as little as possible

Alternatives

\| : match alternative, like the "or" operator

example: /end\(if\|while\|for\)
will match "endif", "endwhile" and "endfor"

Character ranges

/[a-z] : any character from a to z
/[^a-z] : any character which is not from a to z
/[0123456789abcdef]

Special characters to use inside []:
\e : <Esc>
\t : <Tab>
\r : <CR>
\b : <BS>

Predefined ranges

\a : alphabetic characters [a-zA-Z]
\d	digit			[0-9]
\D	non-digit		[^0-9]
\x	hex digit		[0-9a-fA-F]
\X	non-hex digit		[^0-9a-fA-F]
\s	white space		[ 	]     (<Tab> and <Space>)
\S	non-white characters	[^ 	]     (not <Tab> and <Space>)
\l	lowercase alpha		[a-z]
\L	non-lowercase alpha	[^a-z]
\u	uppercase alpha		[A-Z]
\U	non-uppercase alpha	[^A-Z]

These items can not be used inside []

Character classes

\i	identifier characters		'isident'
\I	like \i, excluding digits
\k	keyword characters		'iskeyword'
\K	like \k, excluding digits
\p	printable characters		'isprint'
\P	like \p, excluding digits
\f	file name characters		'isfname'
\F	like \f, excluding digits

Match line break

\n : match line break
example: /the\nword
\s : match space
\_s : match space or line break
\a : match alphabetic character
\_a : match alphabetic character or line break

just add "_" before a range to match for line break

Ag vim regex

[ : becomes \[
] : becomes \]

Escape special characters with "///"
example: '\\\#define foo' will match '#define foo'

All ranges or character classes also need to be escaped
\s : become \\\s
\w : become \\\w
\d : become \\\d
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment