66 |
67 |
68 | 69 |
70 |
71 |

Regex cheatsheet

72 | 73 |

Many programs use regular expression to find & replace text. However, they tend to come with their own different flavor.

74 |

You can probably expect most modern software and programming languages to be using some variation of the Perl flavor, "PCRE"; however command-line tools (grep, less, ...) will often use the POSIX flavor (sometimes with an extended variant, e.g. egrep or sed -r). Vim also comes with its own syntax (a superset of what Vi accepts).

75 |

This cheatsheet lists the respective syntax of each flavor, and the software that uses it.

76 |

If you spot errors or missing data, or just want to make this prettier/more accurate, don't hesitate to open an issue or a pull request.

77 |
78 |
79 | 80 |
81 |

Syntax

82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 |
WhatPerl/PCREPython's rePOSIX (BRE)POSIX extended (ERE)Vim
Basics
Custom character class[...][...][...][...][...]
Negated custom character class[^...][^...][^...][^...][^...]
\ special in class?yesyesno, ] escaped if comes firstno, ] escaped if comes firstyes
Ranges[a-z], - escaped if first or last[a-z], - escaped if first or last[a-z], - escaped if first or last[a-z], - escaped if first or last[a-z], - escaped if first or last
Alternation||\||\| \& (low precedence)
Escaped character\033 \x1B \x{1234} \N{name} \N{U+263D}\x12\%d123 \%x2A \%u1234 \%U1234ABCD
Character classes
Any character (except newline).....
Any character (including newline)\_.
Match a "word" character (alphanumeric plus _)\w [[:word:]]\w\w\w\w
Case[[:upper:]] / [[:lower:]][[:upper:]] / [[:lower:]][[:upper:]] / [[:lower:]]\u [[:upper:]] / \l [[:lower:]]
Match a non-"word" character\W\W\W
Match a whitespace character (except newline)\s [[:space:]]\s [[:space:]]\s [[:space:]]
Whitespace including newline\s [[:space:]]\s\_s
Match a non-whitespace character\S\S[^[:space:]][^[:space:]]\S [^[:space:]]
Match a digit character\d [[:digit:]]\d[[:digit:]][[:digit:]]\d [[:digit:]]
Match a non-digit character\D\D[^[:digit:]][^[:digit:]]\D [^[:digit:]]
Any hexadecimal digit[[:xdigit:]][[:xdigit:]][[:xdigit:]]\x [[:xdigit:]]
Any octal digit\o
Any graphical character excluding "word" characters[[:punct:]][[:punct:]][[:punct:]][[:punct:]]
Any alphabetical character[[:alpha:]][[:alpha:]][[:alpha:]]\a [[:alpha:]]
Non-alphabetical character[^[:alpha:]][^[:alpha:]]\A [^[:alpha:]]
Any alphanumerical character[[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]]
ASCII[[:ascii:]]
Character equivalents (e = é = è) (as per locale)[[=e=]][[=e=]][[=e=]]
Zero-width assertions
Word boundary\b\b\b / \< (start) / \> (end)\b / \< (start) / \> (end)\< (start) / \> (end)
Anywhere but word boundary\B\B\B\B
Beginning of line/string^ / \A^ / \A^^^ (beginning of pattern ) \_^
End of line/string$ / \Z$ / \Z$$$ (end of pattern) \_$
Captures and groups
Capturing group(...) (?<name>...)(...) (?P<name>...)\(...\)(...)\(...\)
Non-capturing group(?:...)(?:...)\%(...\)
Backreference to a specific group.\1 \g1 \g{-1}\1\1\1 non-official\1
Named backreference\g{name} \k<name>(?P=name)
Look-around
Positive look-ahead(?=...)(?=...)\(...\)\@=
Negative look-ahead(?!...)(?!...)\(...\)\@!
Positive look-behind(?<=...)(?<=...)\(...\)\@<=
Negative look-behind(?<!...)(?<!...)\(...\)\@<!
Multiplicity
0 or 1??\??\?
0 or more*****
1 or more+++\+
Specific number{n} {n,m} {n,}{n} {n,m} {n,}\{n\} \{n,m\} \{n,\}{n} {n,m} {n,}\{n} \{n,m} \{n,}
0 or 1, non-greedy????
0 or more, non-greedy*?*?\{-}
1 or more, non-greedy+?+?
Specific number, non-greedy{n,m}? {n,}?{n,m}? {n,}?\{-n,m} \{-n,}
0 or 1, don't give back on backtrack?+
0 or more, don't give back on backtrack*+
1 or more, don't give back on backtrack++
Specific number, don't give back on backtrack{n,m}+ {n,}+
Other
Independent non-backtracking pattern(?>...)\(...\)\@>
Make case-sensitive/insensitive(?i) / (?-i)(?i) / (?-i)\c / \C
159 |
160 | 161 |
162 |

Programs

163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 |
WhatSyntaxComments/gotchas
Programming languages
PerlPCREPCRE is actually a separate implementation from Perl's, with slight differences
Python's re standard libPython's own syntax (Perl-inspired)
RubyRuby's own syntax (Perl-inspired)
Java's java.util.regexAlmost PCRE
Boost.RegexPCRE
Text editors
EclipsePCRE
Emacs?
NetbeansPCRE
Notepad++PCRE (Boost.Regex)
PyCharmPCREPerl-inspired
Sublime Text?
UltraEditPCRE
VimVimIn "magic" mode (default)
Command-line tools
awkEREmight depend on the implementation
grepBRE, egrep for ERE, grep -P for PCRE (optional)
lessEREusually; man page says "regular expression library supplied by your system"
screenplain text
sedBRE, -E switches to ERE
196 |
197 | 198 |
199 | 200 | 227 | 228 |
229 |