Skip to content

Commit 528cee4

Browse files
committed
Regex
1 parent c19c217 commit 528cee4

2 files changed

Lines changed: 32 additions & 32 deletions

File tree

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -361,35 +361,35 @@ Regex
361361

362362
```python
363363
import re
364-
<str> = re.sub(r'<regex>', new, text, count=0) # Substitutes occurrences with string 'new'.
365-
<list> = re.findall(r'<regex>', text) # Returns all occurrences as string objects.
366-
<list> = re.split(r'<regex>', text, maxsplit=0) # Add brackets around regex to keep matches.
367-
<Match> = re.search(r'<regex>', text) # Returns first occ. of the pattern or None.
368-
<Match> = re.match(r'<regex>', text) # Only searches at the start of the 'text'.
369-
<iter> = re.finditer(r'<regex>', text) # Returns all occurrences as Match objects.
364+
<str> = re.sub(r'<regex>', new, text) # Substitutes occurrences with string 'new'.
365+
<list> = re.findall(r'<regex>', text) # Returns all occurrences as string objects.
366+
<list> = re.split(r'<regex>', text) # Add brackets around regex to keep matches.
367+
<Match> = re.search(r'<regex>', text) # Returns first occ. of the pattern or None.
368+
<Match> = re.match(r'<regex>', text) # Only searches at the start of the 'text'.
369+
<iter> = re.finditer(r'<regex>', text) # Returns all occurrences as Match objects.
370370
```
371371

372372
* **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).**
373-
* **Argument 'new' of re.sub() can be a function that accepts Match obj. and returns a string.**
373+
* **Argument `'new'` can also be a function that accepts a Match object and returns a string.**
374374
* **Argument `'flags=re.IGNORECASE'` can be used with all functions that are listed above.**
375375
* **Argument `'flags=re.MULTILINE'` makes `'^'` and `'$'` match the start/end of each line.**
376376
* **Argument `'flags=re.DOTALL'` makes `'.'` also accept the `'\n'` (besides all other chars).**
377377
* **`'re.compile(r"<regex>")'` returns a Pattern object with methods sub(), findall(), etc.**
378378

379379
### Match Object
380380
```python
381-
<str> = <Match>.group() # Returns the whole match. Also group(0).
382-
<str> = <Match>.group(1) # Returns part inside the first brackets.
383-
<tuple> = <Match>.groups() # Returns all bracketed parts as strings.
384-
<int> = <Match>.start() # Returns start index of the whole match.
385-
<int> = <Match>.end() # Returns the match's end index plus one.
381+
<str> = <Match>.group() # Returns the whole match. Also group(0).
382+
<str> = <Match>.group(1) # Returns part inside the first brackets.
383+
<tuple> = <Match>.groups() # Returns all bracketed parts as strings.
384+
<int> = <Match>.start() # Returns start index of the whole match.
385+
<int> = <Match>.end() # Returns the match's end index plus one.
386386
```
387387

388388
### Special Sequences
389389
```python
390-
'\d' == '[0-9]' # Also [०-९…]. Matches decimal character.
391-
'\w' == '[a-zA-Z0-9_]' # Also [ª²³…]. Matches alphanumeric or _.
392-
'\s' == '[ \t\n\r\f\v]' # Also [\x1c-\x1f…]. Matches whitespace.
390+
'\d' == '[0-9]' # Also [०-९…]. Matches decimal character.
391+
'\w' == '[a-zA-Z0-9_]' # Also [ª²³…]. Matches alphanumeric or _.
392+
'\s' == '[ \t\n\r\f\v]' # Also [\x1c-\x1f…]. Matches whitespace.
393393
```
394394
* **By default, decimal characters and alphanumerics from all alphabets are matched unless `'flags=re.ASCII'` is used. It restricts special sequence matches to the first 128 Unicode characters and also prevents `'\s'` from accepting `'\x1c'`, `'\x1d'`, `'\x1e'` and `'\x1f'` (non-printable characters that divide text into files, tables, rows and fields, respectively).**
395395
* **Use a capital letter, i.e. `'\D'`, `'\W'` or `'\S'`, for negation. All non-ASCII characters are matched if ASCII flag is used in conjunction with a capital letter.**

index.html

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
<body>
5858
<header>
59-
<aside>January 8, 2026</aside>
59+
<aside>January 9, 2026</aside>
6060
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
6161
</header>
6262

@@ -343,33 +343,33 @@
343343
&lt;bool&gt; = &lt;str&gt;.isspace() <span class="hljs-comment"># Checks for [ \t\n\r\f\v\x1c\x1d\x1e\x1f\x85…].</span>
344344
</code></pre>
345345
<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-
&lt;str&gt; = re.sub(<span class="hljs-string">r'&lt;regex&gt;'</span>, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes occurrences with string 'new'.</span>
347-
&lt;list&gt; = re.findall(<span class="hljs-string">r'&lt;regex&gt;'</span>, text) <span class="hljs-comment"># Returns all occurrences as string objects.</span>
348-
&lt;list&gt; = re.split(<span class="hljs-string">r'&lt;regex&gt;'</span>, text, maxsplit=<span class="hljs-number">0</span>) <span class="hljs-comment"># Add brackets around regex to keep matches.</span>
349-
&lt;Match&gt; = re.search(<span class="hljs-string">r'&lt;regex&gt;'</span>, text) <span class="hljs-comment"># Returns first occ. of the pattern or None.</span>
350-
&lt;Match&gt; = re.match(<span class="hljs-string">r'&lt;regex&gt;'</span>, text) <span class="hljs-comment"># Only searches at the start of the 'text'.</span>
351-
&lt;iter&gt; = re.finditer(<span class="hljs-string">r'&lt;regex&gt;'</span>, text) <span class="hljs-comment"># Returns all occurrences as Match objects.</span>
346+
&lt;str&gt; = re.sub(<span class="hljs-string">r'&lt;regex&gt;'</span>, new, text) <span class="hljs-comment"># Substitutes occurrences with string 'new'.</span>
347+
&lt;list&gt; = re.findall(<span class="hljs-string">r'&lt;regex&gt;'</span>, text) <span class="hljs-comment"># Returns all occurrences as string objects.</span>
348+
&lt;list&gt; = re.split(<span class="hljs-string">r'&lt;regex&gt;'</span>, text) <span class="hljs-comment"># Add brackets around regex to keep matches.</span>
349+
&lt;Match&gt; = re.search(<span class="hljs-string">r'&lt;regex&gt;'</span>, text) <span class="hljs-comment"># Returns first occ. of the pattern or None.</span>
350+
&lt;Match&gt; = re.match(<span class="hljs-string">r'&lt;regex&gt;'</span>, text) <span class="hljs-comment"># Only searches at the start of the 'text'.</span>
351+
&lt;iter&gt; = re.finditer(<span class="hljs-string">r'&lt;regex&gt;'</span>, text) <span class="hljs-comment"># Returns all occurrences as Match objects.</span>
352352
</code></pre></div>
353353

354354

355355
<ul>
356356
<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>
358358
<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>
359359
<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>
360360
<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>
361361
<li><strong><code class="python hljs"><span class="hljs-string">'re.compile(r"&lt;regex&gt;")'</span></code> returns a Pattern object with methods sub(), findall(), etc.</strong></li>
362362
</ul>
363-
<div><h3 id="matchobject">Match Object</h3><pre><code class="python language-python hljs">&lt;str&gt; = &lt;Match&gt;.group() <span class="hljs-comment"># Returns the whole match. Also group(0).</span>
364-
&lt;str&gt; = &lt;Match&gt;.group(<span class="hljs-number">1</span>) <span class="hljs-comment"># Returns part inside the first brackets.</span>
365-
&lt;tuple&gt; = &lt;Match&gt;.groups() <span class="hljs-comment"># Returns all bracketed parts as strings.</span>
366-
&lt;int&gt; = &lt;Match&gt;.start() <span class="hljs-comment"># Returns start index of the whole match.</span>
367-
&lt;int&gt; = &lt;Match&gt;.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">&lt;str&gt; = &lt;Match&gt;.group() <span class="hljs-comment"># Returns the whole match. Also group(0).</span>
364+
&lt;str&gt; = &lt;Match&gt;.group(<span class="hljs-number">1</span>) <span class="hljs-comment"># Returns part inside the first brackets.</span>
365+
&lt;tuple&gt; = &lt;Match&gt;.groups() <span class="hljs-comment"># Returns all bracketed parts as strings.</span>
366+
&lt;int&gt; = &lt;Match&gt;.start() <span class="hljs-comment"># Returns start index of the whole match.</span>
367+
&lt;int&gt; = &lt;Match&gt;.end() <span class="hljs-comment"># Returns the match's end index plus one.</span>
368368
</code></pre></div>
369369

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>
373373
</code></pre></div>
374374

375375
<ul>
@@ -2933,7 +2933,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29332933

29342934

29352935
<footer>
2936-
<aside>January 8, 2026</aside>
2936+
<aside>January 9, 2026</aside>
29372937
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29382938
</footer>
29392939

0 commit comments

Comments
 (0)