252 lines
9.4 KiB
HTML
252 lines
9.4 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
|
|
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
|
<link rel="stylesheet" href="../assets/css/style.css">
|
|
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
|
<script>
|
|
// Apply theme immediately to prevent flash
|
|
(function() {
|
|
const theme = localStorage.getItem('theme') ||
|
|
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
|
document.documentElement.setAttribute('data-theme', theme);
|
|
})();
|
|
</script>
|
|
<title>regex Cheatsheet - Cheatsheets - Launch Pad</title>
|
|
</head>
|
|
<body>
|
|
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
|
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
|
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
|
</button>
|
|
<br/><br/>
|
|
<div class="name">
|
|
__ _______________________ _________._________________________
|
|
\_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/
|
|
| __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_
|
|
| \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \
|
|
\___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ /
|
|
\/ \/ \/ \/ \/ \/ \/
|
|
</div>
|
|
<div class="blog-page-header">
|
|
<div class="blog-header-content">
|
|
<a href="/cheatsheets" class="back-link" title="Back to Cheatsheets">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
|
</a>
|
|
<h1 class="blog-page-title">regex Cheatsheet</h1>
|
|
</div>
|
|
</div>
|
|
<div class="blog-post-container">
|
|
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
|
|
<div class="blog-post">
|
|
<div class="blog-post-content">
|
|
<p><a href="index.html">← Back to cheatsheets</a></p>
|
|
<p><a href="../index.html">← Home</a></p>
|
|
<hr>
|
|
<p>Regular expressions (regex) are patterns used to match character combinations in strings. This cheatsheet covers common regex syntax with working examples.</p>
|
|
<hr>
|
|
<h2>Basic Patterns</h2>
|
|
<ul>
|
|
<li>`.` - Match any single character (except newline)</li>
|
|
</ul>
|
|
<p>Example: `h.t` matches "hat", "hot", "hit"</p>
|
|
<ul>
|
|
<li>`[abc]` - Match any character in brackets</li>
|
|
</ul>
|
|
<p>Example: `[aeiou]` matches any vowel</p>
|
|
<ul>
|
|
<li>`[^abc]` - Match any character NOT in brackets</li>
|
|
</ul>
|
|
<p>Example: `[^aeiou]` matches any consonant</p>
|
|
<ul>
|
|
<li>`[a-z]` - Match any character in range</li>
|
|
</ul>
|
|
<p>Example: `[0-9]` matches any digit</p>
|
|
<hr>
|
|
<h2>Quantifiers</h2>
|
|
<ul>
|
|
<li>`*` - Zero or more of preceding element</li>
|
|
</ul>
|
|
<p>Example: `ab*c` matches "ac", "abc", "abbc", "abbbc"</p>
|
|
<ul>
|
|
<li>`+` - One or more of preceding element</li>
|
|
</ul>
|
|
<p>Example: `ab+c` matches "abc", "abbc" but not "ac"</p>
|
|
<ul>
|
|
<li>`?` - Zero or one of preceding element (optional)</li>
|
|
</ul>
|
|
<p>Example: `colou?r` matches both "color" and "colour"</p>
|
|
<ul>
|
|
<li>`{n}` - Exactly n occurrences</li>
|
|
</ul>
|
|
<p>Example: `\d{3}` matches exactly 3 digits</p>
|
|
<ul>
|
|
<li>`{n,}` - n or more occurrences</li>
|
|
</ul>
|
|
<p>Example: `\d{3,}` matches 3 or more digits</p>
|
|
<ul>
|
|
<li>`{n,m}` - Between n and m occurrences</li>
|
|
</ul>
|
|
<p>Example: `\d{2,4}` matches 2 to 4 digits</p>
|
|
<hr>
|
|
<h2>Anchors</h2>
|
|
<ul>
|
|
<li>`^` - Start of string (or start of line in multiline mode)</li>
|
|
</ul>
|
|
<p>Example: `^Hello` matches "Hello" only at the start</p>
|
|
<ul>
|
|
<li>`$` - End of string (or end of line in multiline mode)</li>
|
|
</ul>
|
|
<p>Example: `world$` matches "world" only at the end</p>
|
|
<ul>
|
|
<li>`\b` - Word boundary</li>
|
|
</ul>
|
|
<p>Example: `\bcat\b` matches "cat" but not "category"</p>
|
|
<ul>
|
|
<li>`\B` - Non-word boundary</li>
|
|
</ul>
|
|
<p>Example: `\Bcat\B` matches "category" but not "cat"</p>
|
|
<hr>
|
|
<h2>Character Classes</h2>
|
|
<ul>
|
|
<li>`\d` - Any digit (equivalent to `[0-9]`)</li>
|
|
</ul>
|
|
<p>Example: `\d{4}` matches "2024", "1234"</p>
|
|
<ul>
|
|
<li>`\D` - Any non-digit (equivalent to `[^0-9]`)</li>
|
|
</ul>
|
|
<p>Example: `\D+` matches "abc", "xyz"</p>
|
|
<ul>
|
|
<li>`\w` - Any word character (letters, digits, underscore)</li>
|
|
</ul>
|
|
<p>Example: `\w+` matches "hello", "test123", "my_var"</p>
|
|
<ul>
|
|
<li>`\W` - Any non-word character</li>
|
|
</ul>
|
|
<p>Example: `\W+` matches "!!!", " ", "---"</p>
|
|
<ul>
|
|
<li>`\s` - Any whitespace character (space, tab, newline)</li>
|
|
</ul>
|
|
<p>Example: `\s+` matches spaces, tabs</p>
|
|
<ul>
|
|
<li>`\S` - Any non-whitespace character</li>
|
|
</ul>
|
|
<p>Example: `\S+` matches "hello", "123"</p>
|
|
<hr>
|
|
<h2>Groups and Capturing</h2>
|
|
<ul>
|
|
<li>`(abc)` - Capture group</li>
|
|
</ul>
|
|
<p>Example: `(hello)` captures "hello" for backreference</p>
|
|
<ul>
|
|
<li>`(?:abc)` - Non-capturing group</li>
|
|
</ul>
|
|
<p>Example: `(?:hello|hi)` matches but doesn't capture</p>
|
|
<ul>
|
|
<li>`|` - Alternation (OR)</li>
|
|
</ul>
|
|
<p>Example: `cat|dog` matches "cat" or "dog"</p>
|
|
<ul>
|
|
<li>`\1, \2, ...` - Backreferences to captured groups</li>
|
|
</ul>
|
|
<p>Example: `(\w)\1` matches "aa", "bb", "11"</p>
|
|
<hr>
|
|
<h2>Special Characters (Escaping)</h2>
|
|
<ul>
|
|
<li>`\.` - Literal period</li>
|
|
</ul>
|
|
<p>Example: `\.com` matches ".com"</p>
|
|
<ul>
|
|
<li>`\+` - Literal plus sign</li>
|
|
</ul>
|
|
<p>Example: `\+1` matches "+1"</p>
|
|
<ul>
|
|
<li>`\*` - Literal asterisk</li>
|
|
</ul>
|
|
<p>Example: `\*note` matches "*note"</p>
|
|
<ul>
|
|
<li>`\?` - Literal question mark</li>
|
|
</ul>
|
|
<p>Example: `really\?` matches "really?"</p>
|
|
<ul>
|
|
<li>`\(` `\)` - Literal parentheses</li>
|
|
</ul>
|
|
<p>Example: `\(test\)` matches "(test)"</p>
|
|
<hr>
|
|
<h2>Common Patterns</h2>
|
|
<h3>Email Address (Simple)</h3>
|
|
<pre><code>[\w.-]+@[\w.-]+\.\w+</code></pre>
|
|
<p>Matches: `user@example.com`, `test.email@domain.co.uk`</p>
|
|
<h3>Phone Number (US Format)</h3>
|
|
<pre><code>\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}</code></pre>
|
|
<p>Matches: `(555)123-4567`, `555-123-4567`, `555.123.4567`</p>
|
|
<h3>URL</h3>
|
|
<pre><code>https?://[\w.-]+(?:\.[\w.-]+)*(?:/[\w./?=&#-]*)?</code></pre>
|
|
<p>Matches: `https://example.com`, `http://site.com/path?q=1`</p>
|
|
<h3>IP Address</h3>
|
|
<pre><code>\b(?:\d{1,3}\.){3}\d{1,3}\b</code></pre>
|
|
<p>Matches: `192.168.1.1`, `10.0.0.1`</p>
|
|
<h3>Date (MM/DD/YYYY)</h3>
|
|
<pre><code>\d{2}/\d{2}/\d{4}</code></pre>
|
|
<p>Matches: `12/25/2024`, `01/01/2025`</p>
|
|
<hr>
|
|
<h2>Flags/Modifiers (Common)</h2>
|
|
<ul>
|
|
<li>`i` - Case-insensitive matching</li>
|
|
</ul>
|
|
<p>Example: `/hello/i` matches "Hello", "HELLO", "hello"</p>
|
|
<ul>
|
|
<li>`g` - Global matching (find all, not just first)</li>
|
|
</ul>
|
|
<p>Example: `/cat/g` finds all occurrences of "cat"</p>
|
|
<ul>
|
|
<li>`m` - Multiline mode (^ and $ match line boundaries)</li>
|
|
</ul>
|
|
<p>Example: `/^line/m` matches "line" at start of any line</p>
|
|
<ul>
|
|
<li>`s` - Dotall mode (. matches newline)</li>
|
|
</ul>
|
|
<p>Example: `/hello.world/s` matches "hello\nworld"</p>
|
|
<hr>
|
|
<h2>Tips</h2>
|
|
<ul>
|
|
<li>Test regex patterns with online tools like regex101.com or regexr.com</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Escape special characters with backslash when you want literal matches</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use non-greedy quantifiers (`*?`, `+?`) for minimal matches</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Group related patterns with parentheses for better organization</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use anchors (`^`, `$`) to ensure full string matching</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Different tools/languages may have slightly different regex syntax</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Common uses: validation, search/replace, parsing, extraction</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Start simple and build complexity incrementally</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Document complex regex patterns with comments if your tool supports it</li>
|
|
</ul>
|
|
<hr>
|
|
<p><a href="index.html">← Back to cheatsheets</a></p>
|
|
<p><a href="../index.html">← Home</a></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
|
<script src="../theme.js"></script>
|
|
</body>
|
|
</html> |