I have been reading perl regular expression with modifier s m and g. I understand that //g is a global matching where it will be a greedy search.
But I am confused with the modifier s and m. Can anyone explain the difference between s and m with code example to show how it can be different? I have tried to search online and it only gives explanation as in the link http://perldoc.perl.org/perlre.html#Modifiers. In stackoverflow I have even seen people using s and m together. Isn't s is the opposite of m?
//s
//m
//g
I am not able to match multiple line using using m.
use warnings;
use strict;
use 5.012;
my $file;
{
local $/ = undef;
$file = <DATA>;
};
my @strings = $file =~ /".*"/mg; #returns all except the last string across multiple lines
#/"String"/mg; tried with this as well and returns nothing except String
say for @strings;
__DATA__
"This is string"
"1!=2"
"This is \"string\""
"string1"."string2"
"String"
"S
t
r
i
n
g"
Summary: in this tutorial, you are going to learn about Perl regular expression, the most powerful feature of the Perl programming language. A regular expression is a pattern that provides a flexible and concise means to match the string of text. A regular expression is also referred to as regex or regexp.
Perl m Function. Description. This match operator is used to match any keyword in given expression. Parentheses after initial m can be any character and will be used to delimit the regular expression statement.
Code language: Perl (perl) The operator =~ is the binding operator. The whole expression returns a value to indicate whether the regular expression regex was able to match the string successfully. Let’s take a look at an example.
Up to now you’ve noticed that the regular expression engine treats some characters in a special way. These characters are called metacharacters. The following are the metacharacters in Perl regular expressions: {} [] ()^$.|*+?\ To match the literal version of those characters, you have to a backslash \ in front of them in the regular expressions.
/m
and /s
both affect how the match operator treats multi-line strings.
With the /m
modifier, ^
and $
match the beginning and end of any line within the string. Without the /m
modifier, ^
and $
just match the beginning and end of the string.
Example:
$_ = "foo\nbar\n";
/foo$/, /^bar/ do not match
/foo$/m, /^bar/m match
With the /s
modifier, the special character .
matches all characters including newlines. Without the /s
modifier, .
matches all characters except newlines.
$_ = "cat\ndog\ngoldfish";
/cat.*fish/ does not match
/cat.*fish/s matches
It is possible to use /sm
modifiers together.
$_ = "100\n101\n102\n103\n104\n105\n";
/^102.*104$/ does not match
/^102.*104$/s does not match
/^102.*104$/m does not match
/^102.*104$/sm matches
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