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 Expressions Escape Sequences

\ Escape following character
\Q Begin literal sequence
\E End literal sequence

"­Esc­api­ng" is a way of treating characters which have a special meaning in regular expres­sions literally, rather than as special charac­ters.

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 $.

 

Share This Cheat Sheet!

Favourited by 127 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 RyanGannon gamiclea Rober1992 maintainweb jasonlindberg gLes greatghoul sebbu fedesilva slucas Bertrand saji89 vintagecandy1 mryang9118 guslong fire9 schrodervictor DavidLartey imjoshholloway Luracast rd5coding janslow cpettit42 Huyderman pneumatix crss1138 onitek arpitchauhan RboPilot dttk pedromarban cartoonist webpoobah emre 0x90 petraeus avalitan manjuv.gmit parastudio mvotani itbuddha MrOggy85 Epoc akipta Vijay mambero ciphan rajivvishwa ivanfioravanti Dranzz kbenoitcegep alexikia PaulBlp Coldblackice moonrider441 arest erichuang abhisekp Willy Wang gokure Veloz Dxerty nyc.dab ariver jeremic Theosis internaciulo Virviil claysmith sam chen amitb2050 Trpaige001 relizont sandeep mrfatmen GarthZares HappyPaul55 CenturyMan1979 alexpopov ThibaultGomarin Eulerintegral333 caroline.artz vbgenie Keksdose abdulqabiz dafnalula dacheat dandelion_hhv Chilly_Bang leshkin

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.

david david, 08:58 20 May 12

What language/flavor is this? Or I should also ask, if non is specified, what tends to be the default? I'm specifically looking for php or javascript, and I know they're all mostly the same, but not 100%.

Thanks!

Chris Chris, 09:19 7 Jun 12

David, Regex is programming language neutral, as in, it doesn't matter if you are programming regex expressions in javascript, c#, c++, PHP, or even command line *nix, makes no difference. Only thing you have to watch out for is some programming languages may require different various regex characters to be escaped differently (so the programming language doesn't try to interpret it). Usually a backslash. For instance \\ means ONE backslash in many languages.

Andy Grosland Andy Grosland, 09:45 28 Jun 12

Very handy, thank you!

Don't forget Perl ;-)

littleguy littleguy, 15:23 6 Aug 12

Great and useful stuff!

Chilean Chilean, 14:03 23 Aug 12

Hi, I'm trying to learn REGEX, and I need to find this: "Page 1 Of 60", ..... "Page 50 of 60", But I can't find it using reg. expressions! :(. How would you do that? Thank you!

kris w kris w, 08:50 13 Sep 12

Is there a cheat sheet to the cheat sheet? Is this in plain english anywhere?..."negative lookahead"..huh?

Travis Travis, 13:07 4 Oct 12

@david, this cheat sheet is pretty neutral. The most common flavor is Perl Compatible Regular Expressions (PCRE). Javascript's engine is close to that and PHP also has Perl Compatible functions for Regex; they use the PREG prefix. Most everything on this sheet should be supported by PHP's engine (I think POSIX character classes are not). Javascript's engine isn't as featureful. Some advanced features aren't supported, but all the basics are there. If you need a multiline match and you can't use the flag, you can use an inverted class range such as [\s\S] in place of the . (dot) to match anything including newlines.

@Chilean+kris w, You need to find a resource for learning Regular Expressions. This cheat sheet is for reference, not learning. Check out http://www.regular-expressions.info/

fsnow55 fsnow55, 16:17 22 Oct 12

I was confused by the first comment (which was wrong, but you compounded the error with an acknowlegement). ^ is the start of string or line. Period.

Also, your cheat sheet is better organized than the more comprehensive http://www.regular-expressions.info/
since its more succinct. The latter has a 1-page summary but its too verbose.

ty ty, 12:50 22 Nov 12

Nice sheet.

Rob Rob, 11:47 24 Jan 13

Is there a reason why the ']' character is not listed under metacharacters? Doesn't that character require to be escaped if searched for?

Bill Bill, 11:48 24 Jan 13

Hey Dave. Thanks for the cheat sheet. You may want to change "Not a or b or c" when you describe the [^abc} negated character class, because in English, the negation is ambiguous. It could mean "neither a nor be nor c." Or the "a" could be the only negated disjunct. You could mean (~a v (b v c)).

Edir Edir, 09:17 15 Feb 13

Could be added to the list.

Case Conversion
\l Make next character lowercase
\u Make next character uppercase
\L Make entire string (up to \E) lowercase
\U Make entire string (up to \E) uppercase
\u\L Capitalize first char, lowercase rest (sentence)

Gabe Gabe, 17:16 26 Mar 13

I have a database using regex. I am trying to use ^file to get all files with name file_,file ,file_name_date. but it is not working any help

david.baird david.baird, 18:59 15 Jul 13

Is \x supported anywhere? I can't find examples of it in use searching the web. It also does not work in a script on my Macintosh, OSX 10.7.5 using the OS's perl installation.

Drew White Drew White, 15:10 13 Feb 14

I'm trying to come up with a regex string to filter results to a directory that includes a-zA-Z but that also includes an underscore ('_'). Do you know of a way to do this?

David David, 15:14 13 Feb 14

Is \x supported anywhere? I can't seem to find where it is supported. E.g., perl on MacOS 10.7.5. Searching for on-line examples or help also fails, in that no one knows about it.

David David, 15:14 13 Feb 14

Is \x (Regular Expressions Character Classes) supported anywhere? Is this a new class that has just been added, because I am unable to use it in working with IPV6 addresses. Searching for a string containing something like 2001::1a79 with a RegEx 2001::\x{1,4} will fail, but if I use 2001::[a-fA-F0-9]{1,4} will work.

David David, 15:14 13 Feb 14

\x is a term in "Regular Expressions Character Classes" for an hexadecimal digit. How does this compare to the \xhh "Special Characters"? Is it supported today?

Benoit Benoit, 08:54 14 Feb 14

It would be great to increase in some ways the --> : <---- in the
"(?:...) Passive (non-c­apt­uring) group" description. Just after the "?", it is praticaly not visible. Thanks!

Simon Simon, 17:33 17 Feb 14

This is a great cheat-sheet. Two minor niggles:
* Would be great to hint on the characters hidden in the character classes (\s = [ \t\n\r\f], \d = [0-9], \w = [a-zA-Z_0-9])
* I think possibly there's a mistake in the section "Special Characters" - \xxx is probably not the octal character xxx. See "Character Classes": it should be \Oxxx (and by the way: why are \O and \x duplicated in "Special Characters" and "Character Classes"...)
And I support Edir's request for a section "Case Conversion".
But again: great sheet, thanks!

Sahana A V Sahana A V, 17:48 17 Feb 14

Thank you for the Regex cheat sheet :-)

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.