× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Need a cheat sheet for Regex?

Richard Seese
Nov 05, 2015
<p>Tired of trying to constantly search for Regex syntax? Check out below or pay a visit to <a href="http://overapi.com/regex/">OverAPI.com</a> for some really awesome documentation on your favorite programming langague. Includes syntax, docs, and more! I promise the link is adware free. </p> <table border="0" cellpadding="0" cellspacing="0" style="width:100%"> <tbody> <tr> <td> <h3>Anchors</h3> <table border="0" cellpadding="0" cellspacing="0" style="width:301px"> <tbody> <tr> <td> <p>^</p> </td> <td> <p>Start of string, or start of line in multi-line pattern</p> </td> </tr> <tr> <td> <p>\A</p> </td> <td> <p>Start of string</p> </td> </tr> <tr> <td> <p>$</p> </td> <td> <p>End of string, or end of line in multi-line pattern</p> </td> </tr> <tr> <td> <p>\Z</p> </td> <td> <p>End of string</p> </td> </tr> <tr> <td> <p>\b</p> </td> <td> <p>Word boundary</p> </td> </tr> <tr> <td> <p>\B</p> </td> <td> <p>Not word boundary</p> </td> </tr> <tr> <td> <p>\&lt;</p> </td> <td> <p>Start of word</p> </td> </tr> <tr> <td> <p>\&gt;</p> </td> <td> <p>End of word</p> </td> </tr> </tbody> </table> <h3>Character Classes</h3> <table border="0" cellpadding="0" cellspacing="0" style="width:301px"> <tbody> <tr> <td> <p>\c</p> </td> <td> <p>Control character</p> </td> </tr> <tr> <td> <p>\s</p> </td> <td> <p>White space</p> </td> </tr> <tr> <td> <p>\S</p> </td> <td> <p>Not white space</p> </td> </tr> <tr> <td> <p>\d</p> </td> <td> <p>Digit</p> </td> </tr> <tr> <td> <p>\D</p> </td> <td> <p>Not digit</p> </td> </tr> <tr> <td> <p>\w</p> </td> <td> <p>Word</p> </td> </tr> <tr> <td> <p>\W</p> </td> <td> <p>Not word</p> </td> </tr> <tr> <td> <p>\x</p> </td> <td> <p>Hexade­cimal digit</p> </td> </tr> <tr> <td> <p>\O</p> </td> <td> <p>Octal digit</p> </td> </tr> </tbody> </table> <h3>POSIX</h3> <table border="0" cellpadding="0" cellspacing="0" style="width:301px"> <tbody> <tr> <td style="white-space:nowrap"> <p>[:upper:]</p> </td> <td> <p>Upper case letters</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:lower:]</p> </td> <td> <p>Lower case letters</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:alpha:]</p> </td> <td> <p>All letters</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:alnum:]</p> </td> <td> <p>Digits and letters</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:digit:]</p> </td> <td> <p>Digits</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:xdigit:]</p> </td> <td> <p>Hexade­cimal digits</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:punct:]</p> </td> <td> <p>Punctu­ation</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:blank:]</p> </td> <td> <p>Space and tab</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:space:]</p> </td> <td> <p>Blank characters</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:cntrl:]</p> </td> <td> <p>Control characters</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:graph:]</p> </td> <td> <p>Printed characters</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:print:]</p> </td> <td> <p>Printed characters and spaces</p> </td> </tr> <tr> <td style="white-space:nowrap"> <p>[:word:]</p> </td> <td> <p>Digits, letters and underscore</p> </td> </tr> </tbody> </table> <h3>Assertions</h3> <table border="0" cellpadding="0" cellspacing="0" style="width:301px"> <tbody> <tr> <td> <p>?=</p> </td> <td> <p>Lookahead assertion</p> </td> </tr> <tr> <td> <p>?!</p> </td> <td> <p>Negative lookahead</p> </td> </tr> <tr> <td> <p>?&lt;=</p> </td> <td> <p>Lookbehind assertion</p> </td> </tr> <tr> <td> <p>?!= or ?&lt;!</p> </td> <td> <p>Negative lookbehind</p> </td> </tr> <tr> <td> <p>?&gt;</p> </td> <td> <p>Once-only Subexp­ression</p> </td> </tr> <tr> <td> <p>?()</p> </td> <td> <p>Condition [if then]</p> </td> </tr> <tr> <td> <p>?()|</p> </td> <td> <p>Condition [if then else]</p> </td> </tr> <tr> <td> <p>?#</p> </td> <td> <p>Comment</p> </td> </tr> </tbody> </table> </td> <td> </td> <td> <h3>Quanti­fiers</h3> <table border="0" cellpadding="0" cellspacing="0" style="width:301px"> <tbody> <tr> <td> <p>*</p> </td> <td> <p>0 or more</p> </td> <td> <p>{3}</p> </td> <td> <p>Exactly 3</p> </td> </tr> <tr> <td> <p>+</p> </td> <td> <p>1 or more</p> </td> <td> <p>{3,}</p> </td> <td> <p>3 or more</p> </td> </tr> <tr> <td> <p>?</p> </td> <td> <p>0 or 1</p> </td> <td> <p>{3,5}</p> </td> <td> <p>3, 4 or 5</p> </td> </tr> </tbody> </table> <p>Add a ? to a quantifier to make it ungreedy.</p> <h3>Escape Sequences</h3> <table border="0" cellpadding="0" cellspacing="0" style="width:301px"> <tbody> <tr> <td> <p>\</p> </td> <td> <p>Escape following character</p> </td> </tr> <tr> <td> <p>\Q</p> </td> <td> <p>Begin literal sequence</p> </td> </tr> <tr> <td> <p>\E</p> </td> <td> <p>End literal sequence</p> </td> </tr> </tbody> </table> <p>"­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.</p> <h3>Common Metach­ara­cters</h3> <table border="0" cellpadding="0" cellspacing="0" style="width:301px"> <tbody> <tr> <td> <p>^</p> </td> <td> <p>[</p> </td> <td> <p>.</p> </td> <td> <p>$</p> </td> </tr> <tr> <td> <p>{</p> </td> <td> <p>*</p> </td> <td> <p>(</p> </td> <td> <p>\</p> </td> </tr> <tr> <td> <p>+</p> </td> <td> <p>)</p> </td> <td> <p>|</p> </td> <td> <p>?</p> </td> </tr> <tr> <td> <p>&lt;</p> </td> <td colspan="3"> <p>&gt;</p> </td> </tr> </tbody> </table> <p>The escape character is usually \</p> <h3>Special Characters</h3> <table border="0" cellpadding="0" cellspacing="0" style="width:301px"> <tbody> <tr> <td> <p>\n</p> </td> <td> <p>New line</p> </td> </tr> <tr> <td> <p>\r</p> </td> <td> <p>Carriage return</p> </td> </tr> <tr> <td> <p>\t</p> </td> <td> <p>Tab</p> </td> </tr> <tr> <td> <p>\v</p> </td> <td> <p>Vertical tab</p> </td> </tr> <tr> <td> <p>\f</p> </td> <td> <p>Form feed</p> </td> </tr> <tr> <td> <p>\xxx</p> </td> <td> <p>Octal character xxx</p> </td> </tr> <tr> <td> <p>\xhh</p> </td> <td> <p>Hex character hh</p> </td> </tr> </tbody> </table> </td> <td> </td> <td> <h3>Groups and Ranges</h3> <table border="0" cellpadding="0" cellspacing="0" style="width:300px"> <tbody> <tr> <td> <p>.</p> </td> <td> <p>Any character except new line (\n)</p> </td> </tr> <tr> <td> <p>(a|b)</p> </td> <td> <p>a or b</p> </td> </tr> <tr> <td> <p>(...)</p> </td> <td> <p>Group</p> </td> </tr> <tr> <td> <p>(?:...)</p> </td> <td> <p>Passive (non-c­apt­uring) group</p> </td> </tr> <tr> <td> <p>[abc]</p> </td> <td> <p>Range (a or b or c)</p> </td> </tr> <tr> <td> <p>[^abc]</p> </td> <td> <p>Not (a or b or c)</p> </td> </tr> <tr> <td> <p>[a-q]</p> </td> <td> <p>Lower case letter from a to q</p> </td> </tr> <tr> <td> <p>[A-Q]</p> </td> <td> <p>Upper case letter from A to Q</p> </td> </tr> <tr> <td> <p>[0-7]</p> </td> <td> <p>Digit from 0 to 7</p> </td> </tr> <tr> <td> <p>\x</p> </td> <td> <p>Group/­sub­pattern number "­x"</p> </td> </tr> </tbody> </table> <p>Ranges are inclusive.</p> <h3>Pattern Modifiers</h3> <table border="0" cellpadding="0" cellspacing="0" style="width:300px"> <tbody> <tr> <td> <p>g</p> </td> <td> <p>Global match</p> </td> </tr> <tr> <td> <p>i *</p> </td> <td> <p>Case-i­nse­nsitive</p> </td> </tr> <tr> <td> <p>m *</p> </td> <td> <p>Multiple lines</p> </td> </tr> <tr> <td> <p>s *</p> </td> <td> <p>Treat string as single line</p> </td> </tr> <tr> <td> <p>x *</p> </td> <td> <p>Allow comments and whitespace in pattern</p> </td> </tr> <tr> <td> <p>e *</p> </td> <td> <p>Evaluate replac­ement</p> </td> </tr> <tr> <td> <p>U *</p> </td> <td> <p>Ungreedy pattern</p> </td> </tr> </tbody> </table> <p>* PCRE modifier</p> <h3>String Replac­ement</h3> <table border="0" cellpadding="0" cellspacing="0" style="width:300px"> <tbody> <tr> <td> <p>$n</p> </td> <td> <p>nth non-pa­ssive group</p> </td> </tr> <tr> <td> <p>$2</p> </td> <td> <p>"­xyz­" in /^(abc­(xy­z))$/</p> </td> </tr> <tr> <td> <p>$1</p> </td> <td> <p>"­xyz­" in /^(?:a­bc)­(xyz)$/</p> </td> </tr> <tr> <td> <p>$`</p> </td> <td> <p>Before matched string</p> </td> </tr> <tr> <td> <p>$'</p> </td> <td> <p>After matched string</p> </td> </tr> <tr> <td> <p>$+</p> </td> <td> <p>Last matched string</p> </td> </tr> <tr> <td> <p>$&amp;</p> </td> <td> <p>Entire matched string</p> </td> </tr> </tbody> </table> <p>Some regex implem­ent­ations use \ instead of $.</p> </td> </tr> </tbody> </table> <p> </p> <p>(The above cheat sheet is actually from <a href="http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/">http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/</a> because it's easier to copy/paste. OverAPI is still a great bookmark worthy site!</p> <p> </p>
comments powered by Disqus