Sick Gaming
[Tut] Python Regex Methods – A Short Overview - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Python Regex Methods – A Short Overview (/thread-93592.html)



[Tut] Python Regex Methods – A Short Overview - xSicKxBot - 02-14-2020

Python Regex Methods – A Short Overview

<div><p>Python’s <a rel="noreferrer noopener" aria-label="re module (opens in a new tab)" href="https://docs.python.org/3/library/re.html#contents-of-module-re" target="_blank">re module</a> comes with a number of regular expression methods that help you achieve more with less. </p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="Python Regular Expression Methods - An Overview By Example" width="1100" height="619" src="https://www.youtube.com/embed/sWjU442_eYQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>Think of those methods as the framework connecting regular expressions with the <a href="https://blog.finxter.com/start-learning-python/" target="_blank" rel="noreferrer noopener" aria-label="Python programming language (opens in a new tab)">Python programming language</a>. Every programming language comes with its own way of handling regular expressions. For example, the Perl programming language has many built-in mechanisms for regular expressions—you don’t need to import a regular expression library—while the Java programming language provides regular expressions only within a library. This is also the approach of Python.&nbsp;</p>
<p>These are the most important regular expression methods of Python’s re module:</p>
<ul>
<li><code>re.findall(pattern, string)</code>: Checks if the string matches the pattern and returns <strong>all occurrences</strong> of the matched pattern as a list of strings.&nbsp;</li>
<li><code>re.search(pattern, string)</code>: Checks if the string matches the regex pattern and returns only the <strong>first match </strong>as a match object. The match object is just that: an object that stores meta information about the match such as the matching position and the matched substring.</li>
<li><code>re.match(pattern, string)</code>: Checks if any <strong>string prefix</strong> matches the regex pattern and returns a match object.</li>
<li><code>re.fullmatch(pattern, string)</code>: Checks if the <strong>whole string</strong> matches the regex pattern and returns a match object.</li>
<li><code>re.compile(pattern)</code>: Creates a regular expression object from the pattern to speed up the matching if you want to use the regex pattern multiple times.</li>
<li><code>re.split(pattern, string)</code>: Splits the string wherever the pattern regex matches and returns a list of strings. For example, you can split a string into a list of words by using whitespace characters as separators.</li>
<li><code>re.sub(pattern, repl, string)</code>: Replaces (<strong>sub</strong>stitutes) the first occurrence of the regex pattern with the replacement string repl and return a new string.</li>
</ul>
<p>Example: Let’s have a look at some examples of all the above functions:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re text = '''
LADY CAPULET Alack the day, she's dead, she's dead, she's dead! CAPULET Ha! let me see her: out, alas! she's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. Nurse O lamentable day! ''' print(re.findall('she', text)) '''
Finds the pattern 'she' four times in the text: ['she', 'she', 'she', 'she'] ''' print(re.search('she', text)) '''
Finds the first match of 'she' in the text: &lt;re.Match object; span=(34, 37), match='she'> The match object contains important information
such as the matched position. ''' print(re.match('she', text)) '''
Tries to match any string prefix -- but nothing found: None ''' print(re.fullmatch('she', text)) '''
Fails to match the whole string with the pattern 'she': None ''' print(re.split('\n', text)) '''
Splits the whole string on the new line delimiter '\n': ['', 'LADY CAPULET', '', " Alack the day, she's dead, she's dead, she's dead!", '', 'CAPULET', '', " Ha! let me see her: out, alas! she's cold:", ' Her blood is settled, and her joints are stiff;', ' Life and these lips have long been separated:', ' Death lies on her like an untimely frost', ' Upon the sweetest flower of all the field.', '', 'Nurse', '', ' O lamentable day!', ''] ''' print(re.sub('she', 'he', text)) '''
Replaces all occurrences of 'she' with 'he': LADY CAPULET Alack the day, he's dead, he's dead, he's dead! CAPULET Ha! let me see her: out, alas! he's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. Nurse O lamentable day! '''
</pre>
<p>Now, you know the most important regular expression functions. You know how to apply regular expressions to strings. But you don’t know how to write your regex patterns in the first place. Let’s dive into regular expressions and fix this once and for all! </p>
<h2>Where to Go From Here</h2>
<p>You’ve learned a quick overview of the Python regular expression methods. These are the basis of all advanced regex concepts in Python. </p>
<p>Want to learn more about regular expressions and acquire the Python regex superpower? <a href="https://blog.finxter.com/python-regex/">Read my 17,000 word tutorial on the Finxter blog!</a></p>
<p>Need to brush up your Python skills? <a rel="noreferrer noopener" aria-label="Join the free Finxter Computer Science Email Academy! (opens in a new tab)" href="https://blog.finxter.com/subscribe/" target="_blank">Join the free Finxter Computer Science Email Academy!</a></p>
</div>


https://www.sickgaming.net/blog/2020/02/13/python-regex-methods-a-short-overview/