Is there any technical difference between the following code segments in perl? They seem to behave identical
my $str = "A cat is red";
if($str =~ /cat/) {
print "Matches\n";
}
vs
my $str = "A cat is red";
if($str =~ m/cat/) {
print "Matches\n";
}
The difference in this code is the "m" on line 3. Why would someone omit or not omit the "m"?
m operator in Perl is used to match a pattern within the given text. The string passed to m operator can be enclosed within any character which will be used as a delimiter to regular expressions.
A regular expression is a string of characters that defines the pattern or patterns you are viewing. The syntax of regular expressions in Perl is very similar to what you will find within other regular expression. supporting programs, such as sed, grep, and awk.
A \w matches a single alphanumeric character (an alphabetic character, or a decimal digit) or _ , not a whole word. Use \w+ to match a string of Perl-identifier characters (which isn't the same as matching an English word).
The Special Character Classes in Perl are as follows: Digit \d[0-9]: The \d is used to match any digit character and its equivalent to [0-9]. In the regex /\d/ will match a single digit. The \d is standardized to “digit”.
See the RegExp Quote-Like Operators documentation: they're identical. The m
"version" allows you to use other characters instead of /
as a separator. But apart from that, no difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With