Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Perl | Quantifiers in Regular Expression

  • Last Updated : 12 Jun, 2019

Perl provides several numbers of regular expression quantifiers which are used to specify how many times a given character can be repeated before matching is done. This is mainly used when the number of characters going to be matched is unknown.

There are six types of Perl quantifiers which are given below:

  • * = This says the component must be present either zero or more times.
  • + = This says the component must be present either one or more times.
  • ? = This says the component must be present either zero or one time.
  • {n} = This says the component must be present n times.
  • {n, } = This says the component must be present at least n times.
  • {n, m} = This says the component must be present at least n times and no more than m times.

Quantifier Table:
All the above types can be understood with this table which has the regular expression containing quantifiers and their examples.

RegexExamples
/Ax*A/AA, AxA, AxxA, AxxxA, …..
/Ax+A/AxA, AxxA, AxxxA, …..
/Ax?A/AA, AxA
/Ax{1, 3}A/AxA, AxxA, AxxxA
/Ax{2, }A/AxxA, AxxxA, …..
/Ax{4}A/AxxxxA

Let’s see some examples illustrating these quantifiers:
Example-1: In this example, * quantifier is used in a regular expression /Ge*ks/ which produces either “Gks”, “Geks”, “Geeks”…and so on and are being matched with a input string “Gks” and hence it gives “Match Found” as the output.




#!/usr/bin/perl 
     
# Initializing a string 
$a = "Gks";  
       
# matching the above string with "*"
# quantifier in regular expression /Ge*ks/
if ($a =~ m/Ge*ks/)   
{  
    print "Match Found\n";  
}  
else
{  
    print "Match Not Found\n";  
}  

Output:



Match Found

Example-2: In this example, + quantifier is used in a regular expression /Ge+ks/ which produces either “Geks”, “Geeks”, “Geeeks”…and so on and are being matched with a input string “Gks” and hence it gives “Match Not Found” as the output.




#!/usr/bin/perl 
     
# Initializing a string 
$a = "Gks";  
       
# matching the above string with "+"
# quantifier in regular expression /Ge+ks/
if ($a =~ m/Ge+ks/)   
{  
    print "Match Found\n";  
}  
else
{  
    print "Match Not Found\n";  
}  

Output:

Match Not Found

Example-3: In this example, ? quantifier is used in a regular expression /Ge?ks/ which produces either “Geks” or “Gks” and being matched with a input string “Geeks” and hence it gives “Match Not Found” as the output.




#!/usr/bin/perl 
     
# Initializing a string 
$a = "Geeks";  
       
# matching the above string with "?"
# quantifier in regular expression /Ge?ks/
if ($a =~ m/Ge?ks/)   
{  
    print "Match Found\n";  
}  
else
{  
    print "Match Not Found\n";  

Output:

Match Not Found

Example-4: In this example, {n} quantifier is used in a regular expression /Ge{2}ks/ which produces “Geeks” and being matched with a input string “Geeks” and hence it gives “Match Found” as the output.




#!/usr/bin/perl 
     
# Initializing a string 
$a = "Geeks";  
       
# matching the above string with {n}
# quantifier in regular expression /Ge{2}ks/
if ($a =~ m/Ge{2}ks/)   
{  
    print "Match Found\n";  
}  
else
{  
    print "Match Not Found\n";  

Output:

Match Found

Example-5: In this example, {n, } quantifier is used in a regular expression /Ge{2, }ks/ which produces “Geeks”, “Geeeks”, “Geeeeks” and so on and being matched with a input string “Geks” and hence it gives “Match Not Found” as the output.




#!/usr/bin/perl 
     
# Initializing a string 
$a = "Geks";  
       
# matching the above string with {n, }
# quantifier in regular expression /Ge{2, }ks/
if ($a =~ m/Ge{2, }ks/)   
{  
    print "Match Found\n";  
}  
else
{  
    print "Match Not Found\n";  
}  

Output:

Match Not Found

Example-6: In this example, {n, m} quantifier is used in a regular expression /Ge{1, 2}ks/ which produces “Geks” and “Geeks” and are being matched with a input string “Geeeks” and hence it gives “Match Not Found” as the output.




#!/usr/bin/perl 
     
# Initializing a string 
$a = "Geeeks";  
       
# matching the above string with {n, m}
# quantifier in regular expression /Ge{1, 2}ks/
if ($a =~ m/Ge{1, 2}ks/)   
{  
    print "Match Found\n";  
}  
else
{  
    print "Match Not Found\n";  

Output:

Match Not Found



My Personal Notes arrow_drop_up
Recommended Articles
Page :