You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p><em>Above visualization is a screenshot created using</em> <a href="https://www.debuggex.com">debuggex</a> <em>for the pattern</em> <code>r'\bpar(en|ro)?t\b'</code></p>
6326
+
<p><em>Above visualization created using</em> <a href="https://www.debuggex.com">debuggex</a> <em>for the pattern</em> <code>r'\bpar(en|ro)?t\b'</code></p>
<p>A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression</p>
6331
6331
</blockquote>
6332
-
<p>This blog post gives an overview and examples of regular expression syntax as implemented by the <code>re</code> built-in module (Python 3.8+). Assume ASCII character set unless otherwise specified. This post is an excerpt from my <a href="https://github.com/learnbyexample/py_regular_expressions">Python re(gex)?</a> book.</p>
6332
+
<p>This blog post gives an overview and examples of regular expression syntax as implemented by the <code>re</code> built-in module (Python 3.11+). Assume ASCII character set unless otherwise specified. This post is an excerpt from my <a href="https://github.com/learnbyexample/py_regular_expressions">Python re(gex)?</a> book.</p>
6333
+
<br>
6333
6334
<h2 id="elements-that-define-a-regular-expression">Elements that define a regular expression<a class="zola-anchor" href="#elements-that-define-a-regular-expression" aria-label="Anchor link for: elements-that-define-a-regular-expression">🔗</a></h2>
<tr><td><code>\A</code></td><td>restricts the match to the start of string</td></tr>
@@ -6347,9 +6348,9 @@ no VAL is specified, the key will be given the value true.</p>
<tr><td><code>|</code></td><td>multiple RE combined as conditional OR</td></tr>
6349
6350
<tr><td></td><td>each alternative can have independent anchors</td></tr>
6350
-
<tr><td><code>(RE)</code></td><td>group pattern(s), also a capturing group</td></tr>
6351
+
<tr><td><code>(pat)</code></td><td>group pattern(s), also a capturing group</td></tr>
6351
6352
<tr><td></td><td><code>a(b|c)d</code> is same as <code>abd|acd</code></td></tr>
<tr><td><code>.</code></td><td>Match any character except the newline character <code>\n</code></td></tr>
6355
6356
<tr><td><code>[]</code></td><td>Character class, matches one character among many</td></tr>
@@ -6366,7 +6367,7 @@ no VAL is specified, the key will be given the value true.</p>
6366
6367
<tr><td><code>pat1.*pat2</code></td><td>any number of characters between <code>pat1</code> and <code>pat2</code></td></tr>
6367
6368
<tr><td><code>pat1.*pat2|pat2.*pat1</code></td><td>match both <code>pat1</code> and <code>pat2</code> in any order</td></tr>
6368
6369
</tbody></table>
6369
-
<p>Greedy here means that the above quantifiers will match as much as possible that'll also honor the overall RE. Appending a <code>?</code> to greedy quantifiers makes them <strong>non-greedy</strong>, i.e. match as <em>minimally</em> as possible. Quantifiers can be applied to literal characters, groups, backreferences and character classes.</p>
6370
+
<p>Greedy here means that the above quantifiers will match as much as possible that'll also honor the overall RE. Appending a <code>?</code> to greedy quantifiers makes them <strong>non-greedy</strong>, i.e. match as <em>minimally</em> as possible. Appending a <code>+</code> to greedy quantifiers makes them <strong>possessive</strong>, which prevents backtracking. You can also use <code>(?&gt;pat)</code> <strong>atomic grouping</strong> to safeguard from backtracking. Quantifiers can be applied to literal characters, groups, backreferences and character classes.</p>
0 commit comments