Regular Expression Meta-character Digit "d"

What is regular expression meta-character Digit "d"?


In regular expression, the meta-character Digit "d" is used to match any numeric character.

PHP Example:

$name = "23451-9875";
if (preg_match("/d{5}(-d{4})?$/", $name))
echo "Regex Pattern matches!";
echo "Regex Pattern not matched!";
Result :

Pattern matches!

In the above example the pattern "d{5}(-d{4})?$/", first part "d{5}" matches a string with exactly "5" digits and the second part is a sub pattern "(-d{4})?$" matches a "-" along with exactly "4" digits, since the "?" is used for the subpattern it may or may not occur, "$" specifes to check the subpattern at the end of the string.

Perl Example:

#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$name= "-23";
if ($name =~ m/^d+$/)
{print "It's a positive number!";}
{print "It's a negative number!";}
Result :

It's a negative number!

In the above example a positive number is checked for to match, since a negative number is given the condition is false, so it print's the message.

Ask Questions

Ask Question