Skip to content

Instantly share code, notes, and snippets.

@fffaraz
Created December 21, 2014 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fffaraz/2c8f030baaa052b6f833 to your computer and use it in GitHub Desktop.
Save fffaraz/2c8f030baaa052b6f833 to your computer and use it in GitHub Desktop.
Regex Cheat Sheet
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Regex Cheat Sheet</title>
<style type="text/css">
body { font-family: verdana; font-size: smaller; }
h2 { margin-bottom: 3px; font-size: small; }
th { background-color:#f6f6f6; }
td.sc { text-align:center; font-weight:bold; font-size:xx-large; font-family:"courier new", courier; }
</style>
</head>
<body>
<h1>Regular Expressions (Regex) Cheat Sheet</h1>
<h2>Special Characters in Regular Expressions &amp; their meanings</h2>
<table border="1" bordercolor="black" cellspacing="0" cellpadding="8">
<tr>
<th>Character</th>
<th>Meaning</th>
<th>Example</th>
</tr>
<tr>
<td class="sc">*</td>
<td>Match <b>zero, one or more</b> of the previous</td>
<td><code>Ah*</code> matches "<code>Ahhhhh</code>" or "<code>A</code>"</td>
</tr>
<tr>
<td class="sc">?</td>
<td>Match <b>zero or one</b> of the previous</td>
<td><code>Ah?</code> matches "<code>Al</code>" or "<code>Ah</code>"
</tr>
<tr>
<td class="sc">+</td>
<td>Match <b>one or more</b> of the previous</td>
<td><code>Ah+</code> matches "<code>Ah</code>" or "<code>Ahhh</code>" but not "<code>A</code>"</td>
</tr>
<tr>
<td class="sc">\</td>
<td>Used to <b>escape</b> a special character</td>
<td><code>Hungry\?</code> matches "<code>Hungry?</code>"</td>
</tr>
<tr>
<td class="sc">.</td>
<td>Wildcard character, matches <b>any</b> character</td>
<td><code>do.*</code> matches "<code>dog</code>", "<code>door</code>", "<code>dot</code>", etc.</td>
</tr>
<tr>
<td class="sc">( )</td>
<td><b>Group</b> characters</td>
<td>See example for <code>|</code></td>
</tr>
<tr>
<td class="sc">[ ]</td>
<td>Matches a <b>range</b> of characters</td>
<td>
<code>[cbf]ar</code> matches "car", "bar", or "far"<br />
<code>[0-9]+</code> matches any positive integer</br />
<code>[a-zA-Z]</code> matches ascii letters a-z (uppercase and lower case)<br />
<code>[^0-9]</code> matches any character not 0-9.
</tr>
</tr>
<tr>
<td class="sc">|</td>
<td>Matche previous <b>OR</b> next character/group</td>
<td><code>(Mon)|(Tues)day</code> matches "Monday" or "Tuesday"</td>
</tr>
<tr>
<td class="sc">{ }</td>
<td>Matches a specified <b>number of occurrences</b> of the previous</td>
<td>
<code>[0-9]{3}</code> matches "315" but not "31"<br />
<code>[0-9]{2,4}</code> matches "12", "123", and "1234"<br />
<code>[0-9]{2,}</code> matches "1234567..."
</td>
</tr>
<tr>
<td class="sc">^</td>
<td><b>Beginning</b> of a string. Or within a character range <code>[]</code> negation.</td>
<td><code>^http</code> matches strings that begin with http, such as a url.<br /><code>[^0-9]</code> matches any character not 0-9.</td>
</tr>
<tr>
<td class="sc">$</td>
<td><b>End</b> of a string.</td>
<td><code>ing$</code> matches "exciting" but not "ingenious"</td>
</tr>
</table>
<br /><br />
<p>See also: <strong><a href="http://www.petefreitag.com/cheatsheets/regex/character-classes/" title="POSIX Regex Character Classes">Regular Expression Character Classes CheatSheet</a></strong>.</p>
<p><em>This is a work in progress - Questions, comments, criticism, or requests can be directed <a href="/contact/" rel="nofollow">Here</a></em></p>
<p><small><em>Copyright &copy; 2008 <a href="http://www.petefreitag.com/">Peter Freitag</a> (http://www.petefreitag.com/), All Rights Reserved.<br />This document may be printed freely as long as this notice stays intact.</em></small></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment