<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) # 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.
<str> = re.sub(r'<regex>', new, text, count=0) # Substitutes all occurrences with 'new'.
<list> = re.findall(r'<regex>', text) # Returns all occurrences as strings.
<list> = re.split(r'<regex>', text, maxsplit=0) # Add brackets around regex to keep matches.
<Match> = re.search(r'<regex>', text) # First occurrence of the pattern or None.
<Match> = re.match(r'<regex>', text) # Searches only at the beginning of the text.
<iter> = re.finditer(r'<regex>', text) # Returns all occurrences as Match objects.
```
```
* **Argument 'new' can be a function that accepts a Match object and returns a string.**
* **Raw string literals do not interpret escape sequences, thus enabling us to use regex-specific escape sequences that cause SyntaxWarning in normal string literals.**
* **Argument 'new' of re.sub() can be a function that accepts a Match object and returns a str.**
* **Argument `'flags=re.IGNORECASE'` can be used with all functions.**
* **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.MULTILINE'` makes `'^'` and `'$'` match the start/end of each line.**
* **Argument `'flags=re.DOTALL'` makes `'.'` also accept the `'\n'`.**
* **Argument `'flags=re.DOTALL'` makes `'.'` also accept the `'\n'`.**
* **Use `r'\1'` or `'\\1'` for backreference (`'\1'` returns a character with octal code 1).**
* **Add `'?'` after `'*'` and `'+'` to make them non-greedy.**
* **`'re.compile(<regex>)'` returns a Pattern object with methods sub(), findall(), …**
* **`'re.compile(<regex>)'` returns a Pattern object with methods sub(), findall(), …**
### Match Object
### Match Object
```python
```python
<str> = <Match>.group() # Returns the whole match. Also group(0).
<str> = <Match>.group(1) # Returns part inside the first brackets.
<tuple> = <Match>.groups() # Returns all bracketed parts.
<int> = <Match>.start() # Returns start index of the match.
<int> = <Match>.end() # Returns exclusive end index of the match.
<str> = <Match>.group() # Returns the whole match. Also group(0).
<str> = <Match>.group(1) # Returns part inside the first brackets.
<tuple> = <Match>.groups() # Returns all bracketed parts.
<int> = <Match>.start() # Returns start index of the match.
<int> = <Match>.end() # Returns exclusive end index of the match.
```
```
### Special Sequences
### Special Sequences
```python
```python
'\d' == '[0-9]' # Also [०-९…]. Matches a decimal character.
'\w' == '[a-zA-Z0-9_]' # Also [ª²³…]. Matches an alphanumeric or _.
'\s' == '[ \t\n\r\f\v]' # Also [\x1c-\x1f…]. Matches a whitespace.
'\d' == '[0-9]' # Also [०-९…]. Matches a decimal character.
'\w' == '[a-zA-Z0-9_]' # Also [ª²³…]. Matches an alphanumeric or _.
'\s' == '[ \t\n\r\f\v]' # Also [\x1c-\x1f…]. Matches a whitespace.
```
```
* **By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless `'flags=re.ASCII'` argument is used.**
* **By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless `'flags=re.ASCII'` argument is used.**
<div><h2id="regex"><ahref="#regex"name="regex">#</a>Regex</h2><p><strong>Functions for regular expression matching.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> re
<div><h2id="regex"><ahref="#regex"name="regex">#</a>Regex</h2><p><strong>Functions for regular expression matching.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> re
<str> = re.sub(<regex>, new, text, count=<spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># Substitutes all occurrences with 'new'.</span>
<list> = re.findall(<regex>, text) <spanclass="hljs-comment"># Returns all occurrences as strings.</span>
<list> = re.split(<regex>, text, maxsplit=<spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># Add brackets around regex to include matches.</span>
<Match> = re.search(<regex>, text) <spanclass="hljs-comment"># First occurrence of the pattern or None.</span>
<Match> = re.match(<regex>, text) <spanclass="hljs-comment"># Searches only at the beginning of the text.</span>
<iter> = re.finditer(<regex>, text) <spanclass="hljs-comment"># Returns all occurrences as Match objects.</span>
<str> = re.sub(<spanclass="hljs-string">r'<regex>'</span>, new, text, count=<spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># Substitutes all occurrences with 'new'.</span>
<list> = re.findall(<spanclass="hljs-string">r'<regex>'</span>, text) <spanclass="hljs-comment"># Returns all occurrences as strings.</span>
<list> = re.split(<spanclass="hljs-string">r'<regex>'</span>, text, maxsplit=<spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># Add brackets around regex to keep matches.</span>
<Match> = re.search(<spanclass="hljs-string">r'<regex>'</span>, text) <spanclass="hljs-comment"># First occurrence of the pattern or None.</span>
<Match> = re.match(<spanclass="hljs-string">r'<regex>'</span>, text) <spanclass="hljs-comment"># Searches only at the beginning of the text.</span>
<iter> = re.finditer(<spanclass="hljs-string">r'<regex>'</span>, text) <spanclass="hljs-comment"># Returns all occurrences as Match objects.</span>
</code></pre></div>
</code></pre></div>
<ul>
<ul>
<li><strong>Argument 'new' can be a function that accepts a Match object and returns a string.</strong></li>
<li><strong>Raw string literals do not interpret escape sequences, thus enabling us to use regex-specific escape sequences that cause SyntaxWarning in normal string literals.</strong></li>
<li><strong>Argument 'new' of re.sub() can be a function that accepts a Match object and returns a str.</strong></li>
<li><strong>Argument <codeclass="python hljs"><spanclass="hljs-string">'flags=re.IGNORECASE'</span></code> can be used with all functions.</strong></li>
<li><strong>Argument <codeclass="python hljs"><spanclass="hljs-string">'flags=re.IGNORECASE'</span></code> can be used with all functions.</strong></li>
<li><strong>Argument <codeclass="python hljs"><spanclass="hljs-string">'flags=re.MULTILINE'</span></code> makes <codeclass="python hljs"><spanclass="hljs-string">'^'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'$'</span></code> match the start/end of each line.</strong></li>
<li><strong>Argument <codeclass="python hljs"><spanclass="hljs-string">'flags=re.MULTILINE'</span></code> makes <codeclass="python hljs"><spanclass="hljs-string">'^'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'$'</span></code> match the start/end of each line.</strong></li>
<li><strong>Argument <codeclass="python hljs"><spanclass="hljs-string">'flags=re.DOTALL'</span></code> makes <codeclass="python hljs"><spanclass="hljs-string">'.'</span></code> also accept the <codeclass="python hljs"><spanclass="hljs-string">'\n'</span></code>.</strong></li>
<li><strong>Argument <codeclass="python hljs"><spanclass="hljs-string">'flags=re.DOTALL'</span></code> makes <codeclass="python hljs"><spanclass="hljs-string">'.'</span></code> also accept the <codeclass="python hljs"><spanclass="hljs-string">'\n'</span></code>.</strong></li>
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">r'\1'</span></code> or <codeclass="python hljs"><spanclass="hljs-string">'\\1'</span></code> for backreference (<codeclass="python hljs"><spanclass="hljs-string">'\1'</span></code> returns a character with octal code 1).</strong></li>
<li><strong>Add <codeclass="python hljs"><spanclass="hljs-string">'?'</span></code> after <codeclass="python hljs"><spanclass="hljs-string">'*'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'+'</span></code> to make them non-greedy.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'re.compile(<regex>)'</span></code> returns a Pattern object with methods sub(), findall(), …</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'re.compile(<regex>)'</span></code> returns a Pattern object with methods sub(), findall(), …</strong></li>
</ul>
</ul>
<div><h3id="matchobject">Match Object</h3><pre><codeclass="python language-python hljs"><str> = <Match>.group() <spanclass="hljs-comment"># Returns the whole match. Also group(0).</span>
<str> = <Match>.group(<spanclass="hljs-number">1</span>) <spanclass="hljs-comment"># Returns part inside the first brackets.</span>
<tuple> = <Match>.groups() <spanclass="hljs-comment"># Returns all bracketed parts.</span>
<int> = <Match>.start() <spanclass="hljs-comment"># Returns start index of the match.</span>
<int> = <Match>.end() <spanclass="hljs-comment"># Returns exclusive end index of the match.</span>
<div><h3id="matchobject">Match Object</h3><pre><codeclass="python language-python hljs"><str> = <Match>.group() <spanclass="hljs-comment"># Returns the whole match. Also group(0).</span>
<str> = <Match>.group(<spanclass="hljs-number">1</span>) <spanclass="hljs-comment"># Returns part inside the first brackets.</span>
<tuple> = <Match>.groups() <spanclass="hljs-comment"># Returns all bracketed parts.</span>
<int> = <Match>.start() <spanclass="hljs-comment"># Returns start index of the match.</span>
<int> = <Match>.end() <spanclass="hljs-comment"># Returns exclusive end index of the match.</span>
</code></pre></div>
</code></pre></div>
<div><h3id="specialsequences">Special Sequences</h3><pre><codeclass="python language-python hljs"><spanclass="hljs-string">'\d'</span> == <spanclass="hljs-string">'[0-9]'</span><spanclass="hljs-comment"># Also [०-९…]. Matches a decimal character.</span>
<spanclass="hljs-string">'\w'</span> == <spanclass="hljs-string">'[a-zA-Z0-9_]'</span><spanclass="hljs-comment"># Also [ª²³…]. Matches an alphanumeric or _.</span>
<spanclass="hljs-string">'\s'</span> == <spanclass="hljs-string">'[ \t\n\r\f\v]'</span><spanclass="hljs-comment"># Also [\x1c-\x1f…]. Matches a whitespace.</span>
<div><h3id="specialsequences">Special Sequences</h3><pre><codeclass="python language-python hljs"><spanclass="hljs-string">'\d'</span> == <spanclass="hljs-string">'[0-9]'</span><spanclass="hljs-comment"># Also [०-९…]. Matches a decimal character.</span>
<spanclass="hljs-string">'\w'</span> == <spanclass="hljs-string">'[a-zA-Z0-9_]'</span><spanclass="hljs-comment"># Also [ª²³…]. Matches an alphanumeric or _.</span>
<spanclass="hljs-string">'\s'</span> == <spanclass="hljs-string">'[ \t\n\r\f\v]'</span><spanclass="hljs-comment"># Also [\x1c-\x1f…]. Matches a whitespace.</span>