qr/STRING/ Function of Regular Expression in Perl
What is qr/STRING/ Function in Perl?
How to compile a pattern using regular expression functions?
Explanation
The qr/STRING/ function is used to compile a pattern, rather a regular expression that can be used later.If there is a problem with the regular expression one can find it immediately.
Syntax:
qr/STRING/;
Example to compile pattern:
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$pattern='^p';
$re = qr/$pattern/;
$string="people of this town";
if($string =~ /$re/)
{
print "Matched Pattern, string starts with p";
}
else
{
print "String does'nt start with p";
}
Result :
Matched Pattern, string starts with p
In the above code we created a pattern to check whether a string starts with "p", which is stored in the variable "$re", then this variable can be embedded into another pattern or as standlone can be used anywhere. In the above code the pattern is matched to string then the the result is returned.