nonword metacharacter "w" in Regular Expression

How is nonword metacharacter "w" used in regex?

Explanation

The "W" metacharacter is used to match any nonword character, and it is equivalent to the pattern [^A-Za-z0-9] in regular expression.

PHP Example:


<?php
$str = "ABCDabcd_1239*";
if (preg_match("/W/", $str, $matches))
{echo "Pattern matches!";
print "<br>";
echo $matches[0];}
else
{echo "Pattern not matched!";}
?>
Result :

Pattern matches!
*

In the above example for regex the string "$str" has all word characters that are "[a-z][A-Z][0-9]_" and the only nonword character is "*" which is matched.

Perl Example:


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$str = "ABCDabcd_1239";
if ($str =~ m/W/)
{print "It's matched!";}
else
{print "It's not matched!";}
Result :

It's not matched!

In the above example for regex the pattern "/W/" is unmatched, as the string does not have any nonword metacharacter that is "[a-z][A-Z][0-9]_ ".

Ask Questions

Ask Question