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.
#!/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
Recommended Posts:
- Perl | Backtracking in Regular Expression
- Perl | pos() function in Regular Expression
- Perl | 'e' modifier in Regular Expression
- Perl | Operators in Regular Expression
- Kotlin Regular Expression
- Perl | Regular Expressions
- Perl - Use of Capturing in Regular Expressions
- Perl | Special Character Classes in Regular Expressions
- Convert user input string into regular expression using JavaScript
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Ruby | Regular Expressions
- Regular Expressions in Scala
- Perl vs C/C++
- Perl | Classes in OOP
- Perl | GET vs POST in CGI
- Perl | last in loop
- Perl | log() Function
- Perl | oct() Function
- Formats in Perl
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

