Match Begin,End Metacharacter

What is Match Begin,End Metacharacter?

Explanation

The "^" metacharacter matches the beginning of the string, and "$" matches the end of the string.

Example :


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
@fruits = ("orange", "guava", "grapes", "cherry", "berry");
foreach $value(@fruits){
if($value =~ m/^g/)
{
print "$value has 'g' at the begining<br>";
}
}
Result :

guava has 'g' at the begining
grapes has 'g' at the begining

In the above example the strings of an array is checked if the string is starting with the letter "g" and printed using the "^" character.

Example :


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
@fruits = ("orange", "guava", "grapes", "cherry", "berry");
foreach $value(@fruits){
if($value =~ m/y$/)
{
print "$value has 'y' at the end <br>";
}
}
Result :

cherry has 'y' at the end
berry has 'y' at the end

In the above example the strings of the array "fruits" is checked for the letter 'y' at the end of each string and display the matched strings using the "$" character.

Ask Questions

Ask Question