or   Register or Register today to make and share your own cheat sheets! (Why Join?)

Cheatography Login

Join Us!

Not a Cheatographer? Register here!

Social Media

You can login to or register with Cheatography using your Facebook or Twitter account!

Why Join Cheatography?

Make and share cheat sheets!
Join a great community of Cheatographers and add your very own contributions.

Save your favourites!
Quick access to your most loved cheat sheets.

Fewer ads!
Members see no ads on the site.

Coming soon ...
Requests, ratings and more!

Why Join Cheatography?

Make and share cheat sheets!
Join a great community of Cheatographers and add your very own contributions.

Save your favourites!
Quick access to your most loved cheat sheets.

Fewer ads!
Members see no ads on the site.

Coming soon ...
Requests, ratings and more!

Regular Expressions Cheat Sheet by DaveChild

9 Comments   |   Add a Comment   |   PDF Download   |   Find:

Regular Expressions Anchors

^ Start of string, or start of line in multi-line pattern
\A Start of string
$ End of string, or end of line in multi-line pattern
\Z End of string
\b Word boundary
\B Not word boundary
\< Start of word
\> End of word

Regular Expressions Character Classes

\c Control character
\s White space
\S Not white space
\d Digit
\D Not digit
\w Word
\W Not word
\x Hexade­cimal digit
\O Octal digit

Regular Expressions POSIX

[:upper:] Upper case letters
[:lower:] Lower case letters
[:alpha:] All letters
[:alnum:] Digits and letters
[:digit:] Digits
[:xdigit:] Hexade­cimal digits
[:punct:] Punctu­ation
[:blank:] Space and tab
[:space:] Blank characters
[:cntrl:] Control characters
[:graph:] Printed characters
[:print:] Printed characters and spaces
[:word:] Digits, letters and underscore

Regular Expressions Assertions

?= Lookahead assertion
?! Negative lookahead
?<= Lookbehind assertion
?!= or ?<! Negative lookbehind
?> Once-only Subexp­ression
?() Condition [if then]
?()| Condition [if then else]
?# Comment
 

Regular Expressions Quantifiers

* 0 or more
+ 1 or more
? 0 or 1
{3} Exactly 3
{3,} 3 or more
{3,5} 3, 4 or 5

Add a ? to a quantifier to make it ungreedy.

Regular Expression Common Metacharacters

^ [ .
$ { *
( \ +
) | ?
< >  

The escape character is usually the backslash - \.

Regular Expressions Special Characters

\n New line
\r Carriage return
\t Tab
\v Vertical tab
\f Form feed
\xxx Octal character xxx
\xhh Hex character hh
 

Regular Expressions Groups and Ranges

. Any character except new line (\n)
(a|b) a or b
(...) Group
(?:...) Passive (non-c­apt­uring) group
[abc] Range (a or b or c)
[^abc] Not a or b or c
[a-q] Letter from a to q
[A-Q] Upper case letter from A to Q
[0-7] Digit from 0 to 7
\n nth group/­sub­pattern

Ranges are inclusive.

Regular Expressions Pattern Modifiers

g Global match
i Case-i­nse­nsitive
m Multiple lines
s Treat string as single line
x Allow comments and white space in pattern
e Evaluate replac­ement
U Ungreedy pattern

Regular Expressions String Replacement

$n nth non-pa­ssive group
$2 "­xyz­" in /^(abc­(xy­z))$/
$1 "­xyz­" in /^(?:a­bc)­(xyz)$/
$` Before matched string
$' After matched string
$+ Last matched string
$& Entire matched string

Some regex implem­ent­ations use \ instead of $.

Favourited by 37 Members:

bigfox Barry1 michaelfox nschurdell blacklisted chris5marsh CyberTron CITguy DaveChild steinm SamCollett budedub xcjjzh lornajane gissehel arnoldobr anomalophobe manoelhc turborider senseful nolan ouss_sk8 shikee akuiraz GaBBBBB karthikeyansam Winter SubCat7564 jdmyers powareverb loosh naesk Wbbourne provodnik KAA Marty mishin

Comments

Doug Doug, 10:02 28 Nov 11

Your regex cheatsheet says ^ is "Start of string" and $ is "End of string"

DaveChild DaveChild, 10:02 28 Nov 11

Hi Doug. I've clarified that section. Thanks for the heads up :)

, 10:02 28 Nov 11

It looks unchanged to me. ^ still says it's "start of string" and $ still says "end of string". They should be reversed, right?

, 10:02 28 Nov 11

Actually, I'm sorry, you're right! I was pretty confused there, sorry if I've confused anyone else.

DaveChild DaveChild, 10:02 28 Nov 11

No worries Keith - easy mistake to make!

Tom Hunter Tom Hunter, 10:02 28 Nov 11

Can you tag this as 'regex'? Searching for regex on cheatography yields two other results, but not this one.

Doug Doug, 22:52 12 Apr 12

Would you add \Q... \E to the cheatsheet?

Jorge Jorge, 12:16 4 May 12

It would be nice to see the list of white space characters

Jeff Jeff, 15:44 9 May 12

Great resource! Thanks for putting this together and sharing.

Add a Comment

Comment:

Contents

A quick reference guide for regular expres­sions (regex), including symbols, ranges, grouping, assertions and some sample patterns to get you started.

Column Content Comments Author Updated
- Regular Expressions Cheat Sheet DaveChild 24 Nov 11
1 Regular Expressions Anchors 0 DaveChild 25 Oct 11
Regular Expressions Character Classes 0 DaveChild 10 Oct 11
Regular Expressions POSIX 0 DaveChild 10 Oct 11
Regular Expressions Assertions 0 DaveChild 10 Oct 11
2 Regular Expressions Quantifiers 0 DaveChild 10 Oct 11
Regular Expression Common Metacharacters 0 DaveChild 10 Oct 11
Regular Expressions Special Characters 0 DaveChild 10 Oct 11
3 Regular Expressions Groups and Ranges 0 DaveChild 10 Oct 11
Regular Expressions Pattern Modifiers 0 DaveChild 10 Oct 11
Regular Expressions String Replacement 0 DaveChild 10 Oct 11