Regular Expression Operator m// in Perl
What is Regular Expression Operator m// in Perl?
Explanation
m// Operator:
The "m//" supports so many factors,"s" option treats the string being searched as if its a single line,"m" option allows matching of individual lines in a multi line string.If the option "I" is used it matches the string in a case insensitive manner.Another important option is "g" which will have a global scope, it also initialises a pointer to the begining of the string.For every match the pointer moves till the end of the substring, if no match found it will be at the starting point.
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$string = 'average';
if ($string =~ m/^A/i)
{
print "Matched A with a since 'i' option is used.";
}
Result :
Matched A with a since 'i' option is used.
In the above example, we used the "i" option, so "a" is matched with "A" to display a result.
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$string = 'averagenage';
if ($string =~ m/^a/m)
{
print "Matched 'a' in two different lines.";
}
Result :
Matched 'a' in two different lines in a string.
In the above example, we used 'm' option so that the 'a' is matched in two lines as both lines have 'a' at the begining of the string
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$string = 'Fax: 8000-8688';
while ($string =~ m/(d{4})/g)
{
print "'$1' found at position ".(pos($string)-length($1))."<br>";
}
Result :
'8000' found at position 5
'8688' found at position 10
In the above example we have used the 'g' option with the 'm//g' operator, so that using the pointer we can find the position of the strings by negating the current position of the pointer from the length of the string.