Alternate Metacharacter in Perl
What is Alternate Metacharacter in Perl?
Explanation
"|" specifies alternate patterns where matching of either one of them results in a match, usually it checks from left to right and the first one that is matching is taken."|" are used together with parentheses() to indicate the groupings preferred.
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
if ("tom" =~ m/for|if|while/)
{
print "matched charatcer in tom!n";
}
else
{
print "no match in tom!n";
}
print "<br>";
if ("abadc" =~ m/a(a|b|c)a/)
{
print "matched character in abadc!n";
}
else
{
print "no match in abadc!n";
}
Result :
no match in tom!
matched character in abadc!
In the above example the string "tom" is checked for match with "for", "if", "while", since it did'nt match, the string "abadc" is checked for match to have a string "aaa","aba", "aca" , since it contains the character "aba" it returns a match.