Carriage return metacharacter "r" in Regular Expression

How is metacharacter "r" used in regular expression?

Explanation

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

PHP Example:


<?php
$str = "hiox
india";
if (preg_match("/r/", $str, $matches))
echo "Pattern matches!";
else
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!";}
else
{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