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

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

In this tutorial, I’ll show you the difference of the regular expression quantifiers in Python.

What’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 occurrences of `A`.
• Regex `A+` matches one or more occurrences of `A`.

Try it yourself:

## Asterisk vs Question Mark

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.

Analogously, you can read the Python Re A* operator as the zero-or-more regex (I know it sounds a bit clunky): the preceding regex A is matched an arbitrary number of times.

Here’s an example that shows the difference:

```>>> import re
>>> re.findall('ab?', 'abbbbbbb')
['ab']
>>> re.findall('ab*', 'abbbbbbb')
['abbbbbbb']```

The regex ‘ab?’ matches the character ‘a’ in the string, followed by character ‘b’ if it exists (which it does in the code).

The regex ‘ab*’ matches the character ‘a’ in the string, followed by as many characters ‘b’ as possible.

## Asterisk vs Plus

You can read the Python Re A* quantifier as zero-or-more regex: the preceding regex A is matched an arbitrary number of times.

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 too—but at least once.

Here’s an example that shows the difference:

```>>> import re
>>> re.findall('ab*', 'aaaaaaaa')
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
>>> re.findall('ab+', 'aaaaaaaa')
[]```

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.

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.

Summary: 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’.

## Question Mark vs Plus

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.

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.

Here’s an example that shows the difference:

```>>> import re
>>> re.findall('ab?', 'aaaaaaaa')
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
>>> re.findall('ab+', 'aaaaaaaa')
[]```

The regex ‘ab?’ matches the character ‘a’ in the string, followed by character ‘b’ if it exists—but it doesn’t in the code.

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.

## Where to Go From Here?

You’ve learned the difference of the regex quantifiers in Python.

Summary: Regex `A?` matches zero or one occurrences of `A`. Regex `A*` matches zero or more occurrences of `A`. Regex `A+` matches one or more occurrences of `A`.

Want to earn money while you learn Python? Average Python programmers earn more than \$50 per hour. You can become average, can’t you?

Join the free webinar that shows you how to become a thriving coding business owner online!

[Webinar] Are You a Six-Figure Freelance Developer?

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

 Possibly Related Threads… Thread Author Replies Views Last Post [Tut] How To Kill A Thread In Python? xSicKxBot 0 6 Yesterday, 05:40 AM Last Post: xSicKxBot [Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? xSicKxBot 0 12 10-16-2020, 10:30 PM Last Post: xSicKxBot [Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) xSicKxBot 0 18 10-10-2020, 08:24 PM Last Post: xSicKxBot [Tut] Yield Keyword in Python – A Simple Illustrated Guide xSicKxBot 0 19 10-09-2020, 06:13 PM Last Post: xSicKxBot [Tut] Python Reverse List with Slicing — An Illustrated Guide xSicKxBot 0 33 10-02-2020, 04:24 AM Last Post: xSicKxBot [Tut] How to Remove Duplicates From a Python List While Preserving Order? xSicKxBot 0 32 10-01-2020, 02:21 AM Last Post: xSicKxBot [Tut] How To Update A Key In A Dictionary In Python If The Key Doesn’t Exist? xSicKxBot 0 34 09-29-2020, 09:02 PM Last Post: xSicKxBot [Tut] How to Get the Last Element of a Python List? xSicKxBot 0 38 09-28-2020, 02:36 AM Last Post: xSicKxBot [Tut] How To Format A String That Contains Curly Braces In Python? xSicKxBot 0 34 09-25-2020, 07:51 PM Last Post: xSicKxBot [Tut] Python One Line Dictionary xSicKxBot 0 41 09-21-2020, 05:58 PM Last Post: xSicKxBot

Forum Jump:

Become a Patron!
 Upcoming Events

 Latest Threads Xbox Wire - Xbox Insider Release Notes –... Last Post: xSicKxBot Today 02:31 PM » Replies: 0 » Views: 2 News - Video: The lighting technology of... Last Post: xSicKxBot Today 02:30 PM » Replies: 0 » Views: 3 News - Talking Point: What Are You Playi... Last Post: xSicKxBot Today 02:22 PM » Replies: 0 » Views: 2 News - Of Course Scalpers Are Already Li... Last Post: xSicKxBot Today 02:22 PM » Replies: 0 » Views: 2 News - Xbox Could Offer Streaming Stick ... Last Post: xSicKxBot Today 02:21 PM » Replies: 0 » Views: 3 (Indie Deal) FREE Ghost Sweeper, ? NBA 2... Last Post: xSicKxBot Today 08:16 AM » Replies: 0 » Views: 4 (Free Game Key) BigFish Game of Your Cho... Last Post: xSicKxBot Today 08:16 AM » Replies: 0 » Views: 5 News - Random: Not Everyone Is Happy Wit... Last Post: xSicKxBot Today 08:15 AM » Replies: 0 » Views: 3 News - Xbox Could Offer Streaming Stick ... Last Post: xSicKxBot Today 08:15 AM » Replies: 0 » Views: 4 microStudio Game Engine Hands-On Last Post: xSicKxBot Today 12:22 AM » Replies: 0 » Views: 6