{"id":110637,"date":"2020-03-22T10:08:28","date_gmt":"2020-03-22T10:08:28","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=6915"},"modified":"2020-03-22T10:08:28","modified_gmt":"2020-03-22T10:08:28","slug":"python-regex-quantifiers-question-mark-vs-plus-vs-asterisk","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/03\/22\/python-regex-quantifiers-question-mark-vs-plus-vs-asterisk\/","title":{"rendered":"Python Regex Quantifiers \u2013 Question Mark (?) vs Plus (+) vs Asterisk (*)"},"content":{"rendered":"<p>In this tutorial, I&#8217;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>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" 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>\n<\/div>\n<\/figure>\n<p><strong>What&#8217;s the difference between the question mark quantifier (<code>?<\/code>), the plus quantifier (<code>+<\/code>), and the asterisk quantifier (<code>*<\/code>)?<\/strong><\/p>\n<p><strong>Say, you have regular expression pattern <code>A<\/code>. <\/strong><\/p>\n<ul>\n<li><strong>Regex <code>A?<\/code> matches zero or one occurrences of <code>A<\/code>.<\/strong><\/li>\n<li><strong>Regex <code>A*<\/code> matches zero or more occurrences of <code>A<\/code>.<\/strong><\/li>\n<li><strong>Regex <code>A+<\/code> matches one or more occurrences of <code>A<\/code>.<\/strong><\/li>\n<\/ul>\n<p>Try it yourself:<\/p>\n<figure><iframe loading=\"lazy\" src=\"https:\/\/repl.it\/repls\/LuminousSuperficialLoop?lite=true\" allowfullscreen=\"true\" width=\"100%\" height=\"600px\"><\/iframe><\/figure>\n<h2>Asterisk vs Question Mark<\/h2>\n<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&#8217;s not matched more often.<\/p>\n<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>\n<p>Here&#8217;s an example that shows the difference:<\/p>\n<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\n>>> re.findall('ab?', 'abbbbbbb')\n['ab']\n>>> re.findall('ab*', 'abbbbbbb')\n['abbbbbbb']<\/pre>\n<p>The regex &#8216;ab?&#8217; matches the character &#8216;a&#8217; in the string, followed by character &#8216;b&#8217; if it exists (which it does in the code).<\/p>\n<p>The regex &#8216;ab*&#8217; matches the character &#8216;a&#8217; in the string, followed by as many characters &#8216;b&#8217; as possible.<\/p>\n<h2>Asterisk vs Plus<\/h2>\n<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>\n<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&#8212;but at least once.<\/p>\n<p>Here&#8217;s an example that shows the difference:<\/p>\n<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\n>>> re.findall('ab*', 'aaaaaaaa')\n['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']\n>>> re.findall('ab+', 'aaaaaaaa')\n[]<\/pre>\n<p>The regex &#8216;ab*&#8217; matches the character &#8216;a&#8217; in the string, followed by an arbitary number of occurrences of character &#8216;b&#8217;. The substring &#8216;a&#8217; perfectly matches this formulation. Therefore, you find that the regex matches eight times in the string.<\/p>\n<p>The regex &#8216;ab+&#8217; matches the character &#8216;a&#8217;, followed by as many characters &#8216;b&#8217; as possible&#8212;but at least one. However, the character &#8216;b&#8217; does not exist so there&#8217;s no match.<\/p>\n<p><strong>Summary<\/strong>: When applied to regular expression A, Python&#8217;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 &#8216;yes*&#8217; matches strings &#8216;ye&#8217;, &#8216;yes&#8217;, and &#8216;yesssssss&#8217;. But it does not match the empty string because the asterisk quantifier * does not apply to the whole regex &#8216;yes&#8217; but only to the preceding regex &#8216;s&#8217;.<\/p>\n<h2>Question Mark vs Plus<\/h2>\n<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&#8217;s not matched more often.<\/p>\n<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>\n<p>Here&#8217;s an example that shows the difference:<\/p>\n<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\n>>> re.findall('ab?', 'aaaaaaaa')\n['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']\n>>> re.findall('ab+', 'aaaaaaaa')\n[]<\/pre>\n<p>The regex &#8216;ab?&#8217; matches the character &#8216;a&#8217; in the string, followed by character &#8216;b&#8217; if it exists&#8212;but it doesn&#8217;t in the code.<\/p>\n<p>The regex &#8216;ab+&#8217; matches the character &#8216;a&#8217; in the string, followed by as many characters &#8216;b&#8217; as possible&#8212;but at least one. However, the character &#8216;b&#8217; does not exist so there&#8217;s no match.<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>You\u2019ve learned the difference of the regex quantifiers in Python.<\/p>\n<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>\n<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\u2019t you?<\/p>\n<p>Join the free webinar that shows you how to become a thriving coding business owner online!<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\">[Webinar] Are You a Six-Figure Freelance Developer?<\/a><\/p>\n<p>Join us. It\u2019s fun! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, I&#8217;ll show you the difference of the regular expression quantifiers in Python. What&#8217;s the difference between the question mark quantifier (?), the plus quantifier (+), and the asterisk quantifier (*)? Say, you have regular expression pattern A. Regex A? matches zero or one occurrences of A. Regex A* matches zero or more [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-110637","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/110637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=110637"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/110637\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=110637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=110637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=110637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}