Introduction

Regular expressions (regex) are an important concept in programming and mastering them can be extremely beneficial for anyone who wants to work with text data more efficiently. In this tutorial, we will explore the topic of quantifiers in regex, which allow us to specify the number of times a particular pattern should occur in a string. We will be using Python to demonstrate how to use these quantifiers in regular expressions. By the end of this tutorial, you will gain a clear understanding of how to use and apply regex quantifiers in Python. Let's dive in!

Table of Contents :

  • Python Regex Quantifiers
    • Match zero or more times (*)
    • Match one or more times (+)
    • Match zero or one time (?)
    • Match Exactly n Times: {n}
    • Match from n and m times: {n,m}

Python Regex Quantifiers :

  • Quantifiers specify how many times a character or group should be matched in a regular expression.
  • Quantifiers follow a character, character class, or grouping in a regular expression.
  • Different scenarios of using regex quantifiers are : 
    • Match zero or more times (*)
    • Match one or more times (+)
    • Match zero or one time (?)
    • Match Exactly n Times: {n}
    • Match from n and m times: {n,m}

Match zero or more times (*) :

  • The  quantifier matches zero or more occurrences of the preceding character or group.
  • Here's an example of using the  quantifier in a regular expression:
  • Code Sample :

import re

pattern = r"ab*c"
string = "ac abc abbc abbbc abbbbc"
result = re.findall(pattern, string)
print(result)

# The above example will match any occurrence of "ac", "abc", "abbc", "abbbc", and "abbbbc".


Match one or more times (+) :

  • The  quantifier matches one or more occurrences of the preceding character or group.
  • Here's an example of using the  quantifier in a regular expression:
  • Code Sample :

import re

pattern = r"ab+c"
string = "ac abc abbc abbbc abbbbc"
result = re.findall(pattern, string)
print(result)

# The above example will match any occurrence of "abc", "abbc", "abbbc", and "abbbbc".


Match zero or one time (?) :

  • The  quantifier matches zero or one occurrence of the preceding character or group.
  • Here's an example of using the  quantifier in a regular expression:
  • Code Sample :

import re

pattern = r"ab?c"
string = "ac abc abbc abbbc abbbbc"
result = re.findall(pattern, string)
print(result)

# The above example will match any occurrence of "ac", "abc", and "ac".


Match Exactly n Times: {n} :

  • The  {n}  quantifier matches exactly n occurrences of the preceding character or group.
  • Here's an example of using the  {n}  quantifier in a regular expression:
  • Code Sample :

import re

pattern = r"ab{2}c"
string = "ac abc abbc abbbc abbbbc"
result = re.findall(pattern, string)
print(result)

# The above example will match any occurrence of "abbc".



Match at least n times: {n,} :

  • The  {n,}  quantifier matches at least n occurrences of the preceding character or group.
  • Here's an example of using the  {n,}  quantifier in a regular expression:
  • Code Sample :

import re

pattern = r"ab{2,}c"
string = "ac abc abbc abbbc abbbbc"
result = re.findall(pattern, string)
print(result)

# The above example will match any occurrence of "abbc", "abbbc", and "abbbbc".


Match from n and m times: {n,m} :

  • The  {n,m}  quantifier matches between n and m occurrences of the preceding character or group.
  • Here's an example of using the  {n,m}  quantifier in a regular expression:
  • Code Sample :

import re

pattern = r"ab{2,3}c"
string = "ac abc abbc abbbc abbbbc"
result = re.findall(pattern, string)
print(result)

The above example will match any occurrence of "abbc" and "abbbc".


Prev. Tutorial : Word Boundary

Next Tutorial : Greedy Quantifiers