|
|
Carriage return metacharacter "\r" in Regular Expression
|
Tutorials

Regular-expression

|
Topic |
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:\programfiles\perl\bin\perl
print "content-type: text/html\n\n";
$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.
|
| A Note |
Simple Regex Regular Expression Tutorial Online. We welcome your Valuable feedbacks or suggestions. This is a copyright content.
|
|
|
|