Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Regex Quantifiers – Question Mark (?) vs Plus (+) vs Asterisk (*)

#1
Python Regex Quantifiers – Question Mark (?) vs Plus (+) vs Asterisk (*)

<div><p>In this tutorial, I’ll show you the difference of the <a href="https://blog.finxter.com/python-regex/" target="_blank" rel="noreferrer noopener">regular expression</a> quantifiers in Python. </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 Regex Quantifiers - Question Mark (?) vs Plus (+) vs Asterisk (*)" width="1400" height="788" src="https://www.youtube.com/embed/lY25qfgjLBo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p><strong>What’s the difference between the question mark quantifier (<code>?</code>), the plus quantifier (<code>+</code>), and the asterisk quantifier (<code>*</code>)?</strong></p>
<p><strong>Say, you have regular expression pattern <code>A</code>. </strong></p>
<ul>
<li><strong>Regex <code>A?</code> matches zero or one occurrences of <code>A</code>.</strong></li>
<li><strong>Regex <code>A*</code> matches zero or more occurrences of <code>A</code>.</strong></li>
<li><strong>Regex <code>A+</code> matches one or more occurrences of <code>A</code>.</strong></li>
</ul>
<p>Try it yourself:</p>
<figure><iframe src="https://repl.it/repls/LuminousSuperficialLoop?lite=true" allowfullscreen="true" width="100%" height="600px"></iframe></figure>
<h2>Asterisk vs Question Mark</h2>
<p>You can read the Python Re A? quantifier as <a href="https://blog.finxter.com/python-re-question-mark/" target="_blank" rel="noreferrer noopener">zero-or-one regex</a>: the preceding regex A is matched either zero times or exactly once. But it’s not matched more often.</p>
<p>Analogously, you can read the Python Re A* operator as the <a href="https://blog.finxter.com/python-re-asterisk/" target="_blank" rel="noreferrer noopener">zero-or-more regex</a> (I know it sounds a bit clunky): the preceding regex A is matched an arbitrary number of times.</p>
<p>Here’s an example that shows the difference:</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
>>> re.findall('ab?', 'abbbbbbb')
['ab']
>>> re.findall('ab*', 'abbbbbbb')
['abbbbbbb']</pre>
<p>The regex ‘ab?’ matches the character ‘a’ in the string, followed by character ‘b’ if it exists (which it does in the code).</p>
<p>The regex ‘ab*’ matches the character ‘a’ in the string, followed by as many characters ‘b’ as possible.</p>
<h2>Asterisk vs Plus</h2>
<p>You can read the Python Re A* quantifier as zero-or-more regex: the preceding regex A is matched an arbitrary number of times.</p>
<p>Analogously, you can read the Python Re A+ operator as the <a href="https://blog.finxter.com/python-re-plus/" target="_blank" rel="noreferrer noopener">at-least-once regex</a>: the preceding regex A is matched an arbitrary number of times too—but at least once.</p>
<p>Here’s an example that shows the difference:</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
>>> re.findall('ab*', 'aaaaaaaa')
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
>>> re.findall('ab+', 'aaaaaaaa')
[]</pre>
<p>The regex ‘ab*’ matches the character ‘a’ in the string, followed by an arbitary number of occurrences of character ‘b’. The substring ‘a’ perfectly matches this formulation. Therefore, you find that the regex matches eight times in the string.</p>
<p>The regex ‘ab+’ matches the character ‘a’, followed by as many characters ‘b’ as possible—but at least one. However, the character ‘b’ does not exist so there’s no match.</p>
<p><strong>Summary</strong>: When applied to regular expression A, Python’s A* quantifier matches zero or more occurrences of A. The * quantifier is called asterisk operator and it always applies only to the preceding regular expression. For example, the regular expression ‘yes*’ matches strings ‘ye’, ‘yes’, and ‘yesssssss’. But it does not match the empty string because the asterisk quantifier * does not apply to the whole regex ‘yes’ but only to the preceding regex ‘s’.</p>
<h2>Question Mark vs Plus</h2>
<p>You can read the Python Re A? quantifier as zero-or-one regex: the preceding regex A is matched either zero times or exactly once. But it’s not matched more often.</p>
<p>Analogously, you can read the Python Re A+ operator as the at-least-once regex: the preceding regex A is matched an arbitrary number of times but at least once.</p>
<p>Here’s an example that shows the difference:</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
>>> re.findall('ab?', 'aaaaaaaa')
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
>>> re.findall('ab+', 'aaaaaaaa')
[]</pre>
<p>The regex ‘ab?’ matches the character ‘a’ in the string, followed by character ‘b’ if it exists—but it doesn’t in the code.</p>
<p>The regex ‘ab+’ matches the character ‘a’ in the string, followed by as many characters ‘b’ as possible—but at least one. However, the character ‘b’ does not exist so there’s no match.</p>
<h2>Where to Go From Here?</h2>
<p>You’ve learned the difference of the regex quantifiers in Python.</p>
<p><strong>Summary</strong>: <strong>Regex <code>A?</code> matches zero or one occurrences of <code>A</code>. Regex <code>A*</code> matches zero or more occurrences of <code>A</code>. Regex <code>A+</code> matches one or more occurrences of <code>A</code>.</strong></p>
<p><strong>Want to earn money while you learn Python?</strong> Average Python programmers earn more than $50 per hour. You can become average, can’t you?</p>
<p>Join the free webinar that shows you how to become a thriving coding business owner online!</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/">[Webinar] Are You a Six-Figure Freelance Developer?</a></p>
<p>Join us. It’s fun! <img src="https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
</div>


https://www.sickgaming.net/blog/2020/03/...-asterisk/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016