Browse Source

String and Regex update

pull/170/head
Jure Šorn 1 year ago
parent
commit
c41c5b2ad3
2 changed files with 39 additions and 41 deletions
  1. 34
      README.md
  2. 46
      index.html

34
README.md

@ -300,9 +300,11 @@ True
String
------
**Immutable sequence of characters.**
```python
<str> = <str>.strip() # Strips all whitespace characters from both ends.
<str> = <str>.strip('<chars>') # Strips all passed characters from both ends.
<str> = <str>.strip('<chars>') # Strips passed characters. Also lstrip/rstrip().
```
```python
@ -321,6 +323,7 @@ String
```
```python
<str> = <str>.lower() # Changes the case. Also upper/capitalize/title().
<str> = <str>.replace(old, new [, count]) # Replaces 'old' with 'new' at most 'count' times.
<str> = <str>.translate(<table>) # Use `str.maketrans(<dict>)` to generate table.
```
@ -329,38 +332,37 @@ String
<str> = chr(<int>) # Converts int to Unicode character.
<int> = ord(<str>) # Converts Unicode character to int.
```
* **Also: `'lstrip()'`, `'rstrip()'` and `'rsplit()'`.**
* **Also: `'lower()'`, `'upper()'`, `'capitalize()'` and `'title()'`.**
* **Use `'unicodedata.normalize("NFC", <str>)'` on strings that may contain characters like `'Ö'` before comparing them, because they can be stored as one or two characters.**
### Property Methods
```text
+---------------+----------+----------+----------+----------+----------+
| | [ !#$%…] | [a-zA-Z] | [¼½¾] | [²³¹] | [0-9] |
+---------------+----------+----------+----------+----------+----------+
| isprintable() | yes | yes | yes | yes | yes |
| isalnum() | | yes | yes | yes | yes |
| isnumeric() | | | yes | yes | yes |
| isdigit() | | | | yes | yes |
| isdecimal() | | | | | yes |
+---------------+----------+----------+----------+----------+----------+
```python
<bool> = <str>.isdecimal() # Checks for [0-9].
<bool> = <str>.isdigit() # Checks for [²³¹] and isdecimal().
<bool> = <str>.isnumeric() # Checks for [¼½¾] and isdigit().
<bool> = <str>.isalnum() # Checks for [a-zA-Z] and isnumeric().
<bool> = <str>.isprintable() # Checks for [ !#$%…] and isalnum().
<bool> = <str>.isspace() # Checks for [ \t\n\r\f\v\x1c-\x1f\x85\xa0…].
```
* **`'isspace()'` checks for whitespaces: `'[ \t\n\r\f\v\x1c-\x1f\x85\xa0\u1680…]'`.**
Regex
-----
**Functions for regular expression matching.**
```python
import re
```
```python
<str> = re.sub(<regex>, new, text, count=0) # Substitutes all occurrences with 'new'.
<list> = re.findall(<regex>, text) # Returns all occurrences as strings.
<list> = re.split(<regex>, text, maxsplit=0) # Add brackets around regex to include matches.
<Match> = re.search(<regex>, text) # Searches for first occurrence of the pattern.
<Match> = re.search(<regex>, text) # First occurrence of the pattern or None.
<Match> = re.match(<regex>, text) # Searches only at the beginning of the text.
<iter> = re.finditer(<regex>, text) # Returns all occurrences as Match objects.
```
* **Argument 'new' can be a function that accepts a Match object and returns a string.**
* **Search() and match() return None if they can't find a match.**
* **Argument `'flags=re.IGNORECASE'` can be used with all functions.**
* **Argument `'flags=re.MULTILINE'` makes `'^'` and `'$'` match the start/end of each line.**
* **Argument `'flags=re.DOTALL'` makes `'.'` also accept the `'\n'`.**

46
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>October 4, 2023</aside>
<aside>October 11, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -290,10 +290,11 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
┃ decimal.Decimal │ ✓ │ │ │ │ ┃
┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛
</code></pre>
<div><h2 id="string"><a href="#string" name="string">#</a>String</h2><pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.strip() <span class="hljs-comment"># Strips all whitespace characters from both ends.</span>
&lt;str&gt; = &lt;str&gt;.strip(<span class="hljs-string">'&lt;chars&gt;'</span>) <span class="hljs-comment"># Strips all passed characters from both ends.</span>
<div><h2 id="string"><a href="#string" name="string">#</a>String</h2><p><strong>Immutable sequence of characters.</strong></p><pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.strip() <span class="hljs-comment"># Strips all whitespace characters from both ends.</span>
&lt;str&gt; = &lt;str&gt;.strip(<span class="hljs-string">'&lt;chars&gt;'</span>) <span class="hljs-comment"># Strips passed characters. Also lstrip/rstrip().</span>
</code></pre></div>
<pre><code class="python language-python hljs">&lt;list&gt; = &lt;str&gt;.split() <span class="hljs-comment"># Splits on one or more whitespace characters.</span>
&lt;list&gt; = &lt;str&gt;.split(sep=<span class="hljs-keyword">None</span>, maxsplit=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Splits on 'sep' str at most 'maxsplit' times.</span>
&lt;list&gt; = &lt;str&gt;.splitlines(keepends=<span class="hljs-keyword">False</span>) <span class="hljs-comment"># On [\n\r\f\v\x1c-\x1e\x85\u2028\u2029] and \r\n.</span>
@ -305,42 +306,37 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
&lt;int&gt; = &lt;str&gt;.find(&lt;sub_str&gt;) <span class="hljs-comment"># Returns start index of the first match or -1.</span>
&lt;int&gt; = &lt;str&gt;.index(&lt;sub_str&gt;) <span class="hljs-comment"># Same, but raises ValueError if missing.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span>
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.lower() <span class="hljs-comment"># Changes the case. Also upper/capitalize/title().</span>
&lt;str&gt; = &lt;str&gt;.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span>
&lt;str&gt; = &lt;str&gt;.translate(&lt;table&gt;) <span class="hljs-comment"># Use `str.maketrans(&lt;dict&gt;)` to generate table.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;str&gt; = chr(&lt;int&gt;) <span class="hljs-comment"># Converts int to Unicode character.</span>
&lt;int&gt; = ord(&lt;str&gt;) <span class="hljs-comment"># Converts Unicode character to int.</span>
</code></pre>
<ul>
<li><strong>Also: <code class="python hljs"><span class="hljs-string">'lstrip()'</span></code>, <code class="python hljs"><span class="hljs-string">'rstrip()'</span></code> and <code class="python hljs"><span class="hljs-string">'rsplit()'</span></code>.</strong></li>
<li><strong>Also: <code class="python hljs"><span class="hljs-string">'lower()'</span></code>, <code class="python hljs"><span class="hljs-string">'upper()'</span></code>, <code class="python hljs"><span class="hljs-string">'capitalize()'</span></code> and <code class="python hljs"><span class="hljs-string">'title()'</span></code>.</strong></li>
<li><strong>Use <code class="python hljs"><span class="hljs-string">'unicodedata.normalize("NFC", &lt;str&gt;)'</span></code> on strings that may contain characters like <code class="python hljs"><span class="hljs-string">'Ö'</span></code> before comparing them, because they can be stored as one or two characters.</strong></li>
</ul>
<div><h3 id="propertymethods">Property Methods</h3><pre><code class="text language-text">┏━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓
┃ │ [ !#$%…] │ [a-zA-Z] │ [¼½¾] │ [²³¹] │ [0-9] ┃
┠───────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨
┃ isprintable() │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃
┃ isalnum() │ │ ✓ │ ✓ │ ✓ │ ✓ ┃
┃ isnumeric() │ │ │ ✓ │ ✓ │ ✓ ┃
┃ isdigit() │ │ │ │ ✓ │ ✓ ┃
┃ isdecimal() │ │ │ │ │ ✓ ┃
┗━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛
<div><h3 id="propertymethods">Property Methods</h3><pre><code class="python language-python hljs">&lt;bool&gt; = &lt;str&gt;.isdecimal() <span class="hljs-comment"># Checks for [0-9].</span>
&lt;bool&gt; = &lt;str&gt;.isdigit() <span class="hljs-comment"># Checks for [²³¹] and isdecimal().</span>
&lt;bool&gt; = &lt;str&gt;.isnumeric() <span class="hljs-comment"># Checks for [¼½¾] and isdigit().</span>
&lt;bool&gt; = &lt;str&gt;.isalnum() <span class="hljs-comment"># Checks for [a-zA-Z] and isnumeric().</span>
&lt;bool&gt; = &lt;str&gt;.isprintable() <span class="hljs-comment"># Checks for [ !#$%…] and isalnum().</span>
&lt;bool&gt; = &lt;str&gt;.isspace() <span class="hljs-comment"># Checks for [ \t\n\r\f\v\x1c-\x1f\x85\xa0…].</span>
</code></pre></div>
<ul>
<li><strong><code class="python hljs"><span class="hljs-string">'isspace()'</span></code> checks for whitespaces: <code class="python hljs"><span class="hljs-string">'[ \t\n\r\f\v\x1c-\x1f\x85\xa0\u1680…]'</span></code>.</strong></li>
</ul>
<div><h2 id="regex"><a href="#regex" name="regex">#</a>Regex</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> re
&lt;str&gt; = re.sub(&lt;regex&gt;, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes all occurrences with 'new'.</span>
<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
</code></pre></div>
<pre><code class="python language-python hljs">&lt;str&gt; = re.sub(&lt;regex&gt;, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes all occurrences with 'new'.</span>
&lt;list&gt; = re.findall(&lt;regex&gt;, text) <span class="hljs-comment"># Returns all occurrences as strings.</span>
&lt;list&gt; = re.split(&lt;regex&gt;, text, maxsplit=<span class="hljs-number">0</span>) <span class="hljs-comment"># Add brackets around regex to include matches.</span>
&lt;Match&gt; = re.search(&lt;regex&gt;, text) <span class="hljs-comment"># Searches for first occurrence of the pattern.</span>
&lt;Match&gt; = re.search(&lt;regex&gt;, text) <span class="hljs-comment"># First occurrence of the pattern or None.</span>
&lt;Match&gt; = re.match(&lt;regex&gt;, text) <span class="hljs-comment"># Searches only at the beginning of the text.</span>
&lt;iter&gt; = re.finditer(&lt;regex&gt;, text) <span class="hljs-comment"># Returns all occurrences as Match objects.</span>
</code></pre></div>
</code></pre>
<ul>
<li><strong>Argument 'new' can be a function that accepts a Match object and returns a string.</strong></li>
<li><strong>Search() and match() return None if they can't find a match.</strong></li>
<li><strong>Argument <code class="python hljs"><span class="hljs-string">'flags=re.IGNORECASE'</span></code> can be used with all functions.</strong></li>
<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>
<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>.</strong></li>
@ -2929,7 +2925,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the activ
<footer>
<aside>October 4, 2023</aside>
<aside>October 11, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Loading…
Cancel
Save