Regular Expression Meta-character Whitespace "s"

What is regular expression meta-character Whitespace "s"?

Explanation

In Regular Expression, the meta-character "s" is used to match any whitespace like space, tab, form feed etc.

PHP Example:


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

Pattern matches!

In the above example the pattern "/D{5}+s{1}+D{2}/", first matches for "5" non-digit characters, followed by "1" space then by "2" non-digit characters.

Perl Example:


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$name= "EMP.NOIS 123";
if ($name =~ m/s{2,3}/)
{print "It's matched!";}
else
{print "It's not matched!";}
Result :

It's not matched!

In the above example the pattern "s{2,3}/" looks to match atleast "2" spaces upto "3" or more spaces, since the string has only "1" space it is matched.

Ask Questions

Ask Question