Carriage return metacharacter "r" in Regular Expression

How is metacharacter "r" used in regular expression?


In regex, "r" metacharacter is used to match carriage return in a string.

PHP Example:

$str = "hiox
if (preg_match("/r/", $str, $matches))
echo "Pattern matches!";
echo "Pattern not matched!";
Result :

Pattern matches!

In the above regex example the string "$str" has a carriage return, to the next line, so it is matched.

Perl Example:

#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$str = "hi how are you";
if ($str =~ m/r/)
{print "It's matched!";}
{print "It's not matched!";}
Result :

It's not matched!

In the above "r" metacharacter example for regex, there are no "carriage return" in the string, so its unmatched.

Ask Questions

Ask Question