|
56 | 56 |
|
57 | 57 | <body> |
58 | 58 | <header> |
59 | | - <aside>January 8, 2026</aside> |
| 59 | + <aside>January 9, 2026</aside> |
60 | 60 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
61 | 61 | </header> |
62 | 62 |
|
|
343 | 343 | <bool> = <str>.isspace() <span class="hljs-comment"># Checks for [ \t\n\r\f\v\x1c\x1d\x1e\x1f\x85…].</span> |
344 | 344 | </code></pre> |
345 | 345 | <div><h2 id="regex"><a href="#regex" name="regex">#</a>Regex</h2><p><strong>Functions for regular expression matching.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> re |
346 | | -<str> = re.sub(<span class="hljs-string">r'<regex>'</span>, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes occurrences with string 'new'.</span> |
347 | | -<list> = re.findall(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Returns all occurrences as string objects.</span> |
348 | | -<list> = re.split(<span class="hljs-string">r'<regex>'</span>, text, maxsplit=<span class="hljs-number">0</span>) <span class="hljs-comment"># Add brackets around regex to keep matches.</span> |
349 | | -<Match> = re.search(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Returns first occ. of the pattern or None.</span> |
350 | | -<Match> = re.match(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Only searches at the start of the 'text'.</span> |
351 | | -<iter> = re.finditer(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Returns all occurrences as Match objects.</span> |
| 346 | +<str> = re.sub(<span class="hljs-string">r'<regex>'</span>, new, text) <span class="hljs-comment"># Substitutes occurrences with string 'new'.</span> |
| 347 | +<list> = re.findall(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Returns all occurrences as string objects.</span> |
| 348 | +<list> = re.split(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Add brackets around regex to keep matches.</span> |
| 349 | +<Match> = re.search(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Returns first occ. of the pattern or None.</span> |
| 350 | +<Match> = re.match(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Only searches at the start of the 'text'.</span> |
| 351 | +<iter> = re.finditer(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Returns all occurrences as Match objects.</span> |
352 | 352 | </code></pre></div> |
353 | 353 |
|
354 | 354 |
|
355 | 355 | <ul> |
356 | 356 | <li><strong>Raw string literals do not interpret escape sequences, thus enabling us to use the regex-specific escape sequences that cause SyntaxWarning in normal string literals (since 3.12).</strong></li> |
357 | | -<li><strong>Argument 'new' of re.sub() can be a function that accepts Match obj. and returns a string.</strong></li> |
| 357 | +<li><strong>Argument <code class="python hljs"><span class="hljs-string">'new'</span></code> can also be a function that accepts a Match object and returns a string.</strong></li> |
358 | 358 | <li><strong>Argument <code class="python hljs"><span class="hljs-string">'flags=re.IGNORECASE'</span></code> can be used with all functions that are listed above.</strong></li> |
359 | 359 | <li><strong>Argument <code class="python hljs"><span class="hljs-string">'flags=re.MULTILINE'</span></code> makes <code class="python hljs"><span class="hljs-string">'^'</span></code> and <code class="python hljs"><span class="hljs-string">'$'</span></code> match the start/end of each line.</strong></li> |
360 | 360 | <li><strong>Argument <code class="python hljs"><span class="hljs-string">'flags=re.DOTALL'</span></code> makes <code class="python hljs"><span class="hljs-string">'.'</span></code> also accept the <code class="python hljs"><span class="hljs-string">'\n'</span></code> (besides all other chars).</strong></li> |
361 | 361 | <li><strong><code class="python hljs"><span class="hljs-string">'re.compile(r"<regex>")'</span></code> returns a Pattern object with methods sub(), findall(), etc.</strong></li> |
362 | 362 | </ul> |
363 | | -<div><h3 id="matchobject">Match Object</h3><pre><code class="python language-python hljs"><str> = <Match>.group() <span class="hljs-comment"># Returns the whole match. Also group(0).</span> |
364 | | -<str> = <Match>.group(<span class="hljs-number">1</span>) <span class="hljs-comment"># Returns part inside the first brackets.</span> |
365 | | -<tuple> = <Match>.groups() <span class="hljs-comment"># Returns all bracketed parts as strings.</span> |
366 | | -<int> = <Match>.start() <span class="hljs-comment"># Returns start index of the whole match.</span> |
367 | | -<int> = <Match>.end() <span class="hljs-comment"># Returns the match's end index plus one.</span> |
| 363 | +<div><h3 id="matchobject">Match Object</h3><pre><code class="python language-python hljs"><str> = <Match>.group() <span class="hljs-comment"># Returns the whole match. Also group(0).</span> |
| 364 | +<str> = <Match>.group(<span class="hljs-number">1</span>) <span class="hljs-comment"># Returns part inside the first brackets.</span> |
| 365 | +<tuple> = <Match>.groups() <span class="hljs-comment"># Returns all bracketed parts as strings.</span> |
| 366 | +<int> = <Match>.start() <span class="hljs-comment"># Returns start index of the whole match.</span> |
| 367 | +<int> = <Match>.end() <span class="hljs-comment"># Returns the match's end index plus one.</span> |
368 | 368 | </code></pre></div> |
369 | 369 |
|
370 | | -<div><h3 id="specialsequences">Special Sequences</h3><pre><code class="python language-python hljs"><span class="hljs-string">'\d'</span> == <span class="hljs-string">'[0-9]'</span> <span class="hljs-comment"># Also [०-९…]. Matches decimal character.</span> |
371 | | -<span class="hljs-string">'\w'</span> == <span class="hljs-string">'[a-zA-Z0-9_]'</span> <span class="hljs-comment"># Also [ª²³…]. Matches alphanumeric or _.</span> |
372 | | -<span class="hljs-string">'\s'</span> == <span class="hljs-string">'[ \t\n\r\f\v]'</span> <span class="hljs-comment"># Also [\x1c-\x1f…]. Matches whitespace.</span> |
| 370 | +<div><h3 id="specialsequences">Special Sequences</h3><pre><code class="python language-python hljs"><span class="hljs-string">'\d'</span> == <span class="hljs-string">'[0-9]'</span> <span class="hljs-comment"># Also [०-९…]. Matches decimal character.</span> |
| 371 | +<span class="hljs-string">'\w'</span> == <span class="hljs-string">'[a-zA-Z0-9_]'</span> <span class="hljs-comment"># Also [ª²³…]. Matches alphanumeric or _.</span> |
| 372 | +<span class="hljs-string">'\s'</span> == <span class="hljs-string">'[ \t\n\r\f\v]'</span> <span class="hljs-comment"># Also [\x1c-\x1f…]. Matches whitespace.</span> |
373 | 373 | </code></pre></div> |
374 | 374 |
|
375 | 375 | <ul> |
@@ -2933,7 +2933,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment |
2933 | 2933 |
|
2934 | 2934 |
|
2935 | 2935 | <footer> |
2936 | | - <aside>January 8, 2026</aside> |
| 2936 | + <aside>January 9, 2026</aside> |
2937 | 2937 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
2938 | 2938 | </footer> |
2939 | 2939 |
|
|
0 commit comments