Regex Cheat Sheet

Probably the Best Regular Expression Cheat Sheet on the Net

If you like this page, please

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

Download as PDF

Metacharacter Meaning
\n Newline
[...] Range or character class
[^...] Not in range or negated character class
. (dot or point) Any character except newline
\w Word character [a-zA-Z0-9_]
\W Nonword character [^a-zA-Z0-9_]
\d Digit character [0-9]
\D Nondigit character [^0-9]
\s Whitespace character [\n\r\f\t]
\S Nonwhitespace character [^\n\r\f\t]
^ (caret) The start of the line of text
$ (dollar) The end of the line of text
\b Word boundary
\B Not-word-boundary
i Case-insensitive matching
m ^ and $ match next to embedded \n
(...) Group subpattern and capture submatch into \1, \2, ..
\n Contains 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

Metacharacters Meaning Python PHP Perl JavaScript Ruby
\a Alert
\b Backspace
\n Newline
\r Carriage return
\f Form feed
\t Horizontal tab
\octal Character 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
\w Word character [a-zA-Z0-9_]
\W Nonword character [^a-zA-Z0-9_]
\d Digit character [0-9]
\D Nondigit character [^0-9]
\s Whitespace character [\n\r\f\t]
\S Nonwhitespace character [^\n\r\f\t]
^ (caret) The start of the line of text
\A Start of search string, in all match modes
$ (dollar) The end of the line of text
\Z End of string, or the point before a string-ending newline, in any match mode
\b Word boundary
\B Not-word-boundary
(?=...) Positive lookahead
(?!...) Negative lookahead
(?<=...) Positive lookbehind
(?<!...) Negative lookbehind
i Case-insensitive matching
m ^ and $ match next to embedded \n
s Dot (.) matches newline
x Ignore 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, ..
\n Contains 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

PHP

© 2017 RegexCheatSheet.com - Regular Expressions Cheat Sheet for Python, PHP, Perl, JavaScript, Ruby.