Regex Cheat Sheet

Probably the Best Regular Expression Cheat Sheet on the Net

The most commonly used metacharacters in Python, PHP, Perl, JavaScript, and Ruby

Download as PDF

MetacharacterMeaning
\nNewline
[…]Range or character class
[^…]Not in range or negated character class
.(dot or point) Any character except newline
\wWord character [a-zA-Z0-9_]
\WNonword character [^a-zA-Z0-9_]
\dDigit character [0-9]
\DNondigit character [^0-9]
\sWhitespace character [\n\r\f\t]
\SNonwhitespace character [^\n\r\f\t]
^(caret) The start of the line of text
$(dollar) The end of the line of text
\bWord boundary
\BNot-word-boundary
iCase-insensitive matching
m^ and $ match next to embedded \n
(…)Group subpattern and capture submatch into \1, \2, ..
\nContains the result of nth earlier submatch from a parentheses capture group, or a named capture group
*(asterisk or star) Match 0 or more times
+(plus) Match 1 or more times
?(question mark) Match 1 or 0 times
{n}Match exactly n times

Python, PHP, Perl, JavaScript, and Ruby Supported Metacharacters

Download as PDF

MetacharactersMeaningPythonPHPPerlJavaScriptRuby
\aAlert
\bBackspace
\nNewline
\rCarriage return
\fForm feed
\tHorizontal tab
\octalCharacter specified by a three-digit octal code
[…]Range or character class
[^…]Not in range or negated character class
.(dot or point) Any character except newline
\wWord character [a-zA-Z0-9_]
\WNonword character [^a-zA-Z0-9_]
\dDigit character [0-9]
\DNondigit character [^0-9]
\sWhitespace character [\n\r\f\t]
\SNonwhitespace character [^\n\r\f\t]
^(caret) The start of the line of text
\AStart of search string, in all match modes
$(dollar) The end of the line of text
\ZEnd of string, or the point before a string-ending newline, in any match mode
\bWord boundary
\BNot-word-boundary
(?=…)Positive lookahead
(?!…)Negative lookahead
(?<=…)Positive lookbehind
(?<!…)Negative lookbehind
iCase-insensitive matching
m^ and $ match next to embedded \n
sDot (.) matches newline
xIgnore whitespace, and allow comments (#) in pattern
(?mode)Turn list modes on for the rest of the subexpression
(?#…)Treat substring as a comment
#…Rest of the line is treated as a comment in x mode
(…)Group subpattern and capture submatch into \1, \2, ..
\nContains the result of nth earlier submatch from a parentheses capture group, or a named capture group
(?:…)Groups subpattern, but does not capture submatch
…|…Try subpatterns in alternation
*(asterisk or star) Match 0 or more times
+(plus) Match 1 or more times
?(question mark) Match 1 or 0 times
{n}Match exactly n times
{n,}Match at least n times
{x,y}Match at least x times, but no more than y times
*?Match 0 or more times, but a few times as possible
+?Match 1 or more times, but a few times as possible
??Match 0 or 1 times, but as few times as possible
{x,y}?Match at least x times, no more than y times, and as few times as possible

Useful Links