Match Any Metacharacter in Perl

What is Match Any Metacharacter in Perl?

Explanation

The "." metacharacter matches all characters except any newline characters in a multi-line string. If the "s" option of "m//s" is used, embedded newline characters will also be matched. This is convenient to match a pattern in multiple lines.

Example :


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
if ("anbnc" =~ m/a.b/)
{
print "matched the string anbnc!n";
}
else
{
print "no match in the string anbnc!n";
}
print "<br>";
if ("anbnc" =~ m/a.*c/s)
{
print "matched the string anbnc!n";
}
else
{
print "no match in the string anbnc!n";
}
Result :

no match in the string a b c!
matched the string a b c!

In the above example the string "anbnc" is matched with the pattern m/a.*c/s, since the newline characters are present in between the pattern it gives a mismatch as m// does'nt match newline characters, but when the "s" option is used even if the embedded newline characters are present pattern is matched.

Ask Questions

Ask Question