Skip to main content
  1. Posts/

Sublime Text Regular Expression Cheat Sheet

··620 words·3 mins·
Table of Contents

A cheat sheet about regular expressions used in Sublime Text.

Special characters
#

expressionDescription
.Match any character
^Match line begin
$Match line end
*Match previous RE 0 or more times greedily
*?Match previous RE 0 or more times non-greedily
+Match previous RE 1 or more times greedily
+?Match previous RE 1 or more times non-greedily
?Match previous RE 0 or 1 time greedily
??Match previous RE 0 or 1 time non-greedily
A|BMatch either RE A or B
{m}Match previous RE exactly m times
{m,n}Match previous RE m to n times greedily
{m, n}?Match previous RE m to n times, non-greedily

Character set
#

expressionDescription
[abc]Match either a, b or c
[^abc]Match any character not in this set (i.e., not a, b and c)
[a-z]Match the range from a to z
[a-f2-8]Match the range from a to f or the range from 2 to 8
[a\-z]Match a, - or z
[a-]Match a, -
[-a]Match -, a
[{}*|()[]+\^$.?]Match either one of the chacters in []{}*|()+^$?.
  • Note that you can also use character class inside [], for example, [\w] matches any character in word character class.

Character class
#

“Multiple character” character class
#

An expression of the form [[:name:]] matches the named character class name.

class nameDescription
alnumAny alpha-numeric character
alphaAny alphabetic character.
digitAny decimal digit.
xdigitAny hexadecimal digit character.
lowerAny lower case character.
upperAny upper case character.
cntrlAny control character1.
printAny printable character.
punctAny punctuation character. 2
spaceAny whitespace character. 3
wordAny word character (alphanumeric characters plus the underscore).

Note: To use upper and lower, you have to enable case sensitve search.

“Single character” character class
#

class nameDescription
\dEqual to [[:digit:]]
\lEqual to [[:lower:]]
\uEqual to [[:upper:]]
\sEqual to [[:space:]]
\wEqual to [[:word:]]
\DEqual to [^[:digit:]]
\LEqual to [^[:lower:]]
\UEqual to [^[:upper:]]
\WEqual to [^[:word:]]

Regex groups
#

Defining capture groups
#

expressionDescription
(?<NAME>pattern)Define a regex group named NAME which you can later refer to with \g{NAME}
(?=pattern)Positive lookahead, consumes zero characters, the preceding RE only matches if this matches
(?!pattern)Negative lookahead, consumes zero characters, the preceding RE only matches if this does not match
(?<=pattern)Positive lookbehind, consumes zero characters, the following RE will only match if preceded with this fixed length RE.
(?<!pattern)Negative lookbehind, consumes zero characters, the following RE will only match if not preceded with this fixed length RE.

Refering to matching groups (capture groups)
#

expressionDescription
\1Refer to first regex group
\g{1}Refer to first regex group
\g{12}Refer to 12th regex group
\g{-1}Refer to last regex group
\g{-2}Refer to last but one regex group
  • The regex groups are indexed by the order of their opening braces.
  • Note the \g{NUM} form allows for matching regex group index larger than 9, for example, \g{12}.

Miscellaneous
#

Escapes
#

class nameDescription
\xddA hexadecimal escape sequence - matches the single character whose code point is 0xdd.
\x{dddd}A hexadecimal escape sequence - matches the single character whose code point is 0xdddd.

Word boundaries
#

The following escape sequences match the boundaries of words:

class nameDescription
\<Matches the start of a word.
\>Matches the end of a word.
\bMatches a word boundary (the start or end of a word).
\BMatches only when not at a word boundary.

References
#

The title image is taken from here.

Related

Learning Expect Programming
··988 words·5 mins
How Is Newline Handled in Python and Various Editors?
··1035 words·5 mins
A Complete Guide on Writing and Building C++ Programs in Sublime Text
··1114 words·6 mins