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.
| Regex |
Examples |
| /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.
$a = "Gks";
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.
$a = "Gks";
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.
$a = "Geeks";
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.
$a = "Geeks";
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.
$a = "Geks";
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.
$a = "Geeeks";
if ($a =~ m/Ge{1, 2}ks/)
{
print "Match Found\n";
}
else
{
print "Match Not Found\n";
}
|
Output:
Match Not Found
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
12 Jun, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...