Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between m/PATTERN/ and /PATTERN/ in perl

Tags:

regex

match

perl

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"?

like image 619
user3087867 Avatar asked Dec 22 '13 20:12

user3087867


People also ask

What does M mean in Perl regex?

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.

What is pattern in Perl?

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.

What is the use \w in Perl?

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).

What does \d mean in Perl?

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”.


1 Answers

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.

like image 195
Mat Avatar answered Sep 30 '22 17:09

Mat