Regular Expression Meta-character New Line "n"

What is regular expression meta-character New Line "n"?

Explanation

In Regular Expression, the meta-character "n" is used to match the new line character.

PHP Example:


<?php
$emp = "empno";
if (preg_match("/D{5}(n{1})?$/", $emp))
echo "Regex Pattern matches!";
else
echo "Regex Pattern not matched!";
?>
Result :

Pattern matches!

In the above example the pattern "/D{5}(n{1})?$/",first part "D{5}" matches a string with "5" characters ,the second part or the sub pattern macthes a single "n" character, since the "?" even a match occurs not matter "n" is present or not.

Perl Example:


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$name= "andern";
if ($name =~ m/D{5}n$/)
{print "It's matched!";}
else
{print "It's not matched!";}
Result :

It's matched!

In the above example the pattern matches for exactly "5" non digit characters and should end with a "n" character and in the example the string matches.


Ask Questions

Ask Question