Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Quantifiers in Perl's regular expressions determine how many instances of a character, group, or character class must be present in the input for a match to be found. They play a critical role in pattern matching, allowing for flexibility in defining how much or how little of a specific pattern you're looking to find.
Here's a tutorial on Perl's regular expression quantifiers:
*
- Star: Matches 0 or more of the preceding character or group.
"abc" =~ /ab*c/; # Matches 'ac', 'abc', 'abbc', 'abbbc', etc.
+
- Plus: Matches 1 or more of the preceding character or group.
"abc" =~ /ab+c/; # Matches 'abc', 'abbc', 'abbbc', etc., but not 'ac'
?
- Question Mark: Matches 0 or 1 of the preceding character or group, making it optional.
"color" =~ /colou?r/; # Matches both 'color' and 'colour'
{n}
: Matches exactly n
instances of the preceding character or group.
"abbbc" =~ /ab{3}c/; # Matches 'abbbc' but not 'abc' or 'abbc'
{n,}
: Matches n
or more instances.
"abbbc" =~ /ab{2,}c/; # Matches 'abbc', 'abbbc', 'abbbbc', etc.
{n,m}
: Matches between n
and m
instances.
"abbbc" =~ /ab{2,3}c/; # Matches 'abbc' and 'abbbc' but not 'abc' or 'abbbbc'
By default, quantifiers in Perl regex are greedy, meaning they'll match as much as they can. You can make them non-greedy by appending a ?
:
Greedy:
"abbbbcd" =~ /ab+c/; # Matches 'abbbbc'
Non-Greedy:
"abbbbcd" =~ /ab+?c/; # Matches 'abc'
Quantifiers can be used with character classes and groups:
Character Classes:
"a3d" =~ /[0-9]{2,3}/; # Would match '123' in "a123d", but not '3' in "a3d"
Groups:
Groups are formed using parentheses ()
.
"hello hello hello" =~ /(hello ){3}/; # Matches 'hello hello hello '
Quantifiers are a powerful aspect of Perl's regex capabilities, allowing for versatile pattern matching. With a good understanding of their behavior and potential pitfalls, you can leverage them effectively in your Perl scripts.
my $text = "abbbb"; $text =~ /a.*b/; # Greedy quantifier print "Greedy: $&\n"; # Outputs: Greedy: abbbb $text =~ /a.*?b/; # Non-greedy quantifier print "Non-greedy: $&\n"; # Outputs: Non-greedy: ab
my $pattern = "a{2,4}"; my $text = "aa"; if ($text =~ /$pattern/) { print "Matched: $&\n"; # Outputs: Matched: aa }
my $text = "ab"; $text =~ /a*b/; print "Matched: $&\n"; # Outputs: Matched: ab
my $text = "aaab"; $text =~ /a+b/; print "Matched: $&\n"; # Outputs: Matched: aaab
my $text = "aaaa"; $text =~ /a{2}/; print "Matched: $&\n"; # Outputs: Matched: aa $text =~ /a{2,4}/; print "Matched: $&\n"; # Outputs: Matched: aaaa
my $text = "a{3}"; $text =~ /a\{3}/; print "Matched: $&\n"; # Outputs: Matched: a{3}
my $text = "aaabbb"; $text =~ /a{2,4}b{2,}/; print "Matched: $&\n"; # Outputs: Matched: aaabbb