Word Boundry "b" metacharacter in Regular Expression.

What is Word Boundry?


A word boundry is the boundry between a word character and a non-word character.The word characters are "[a-zA-Z0-9_]" all other characters are non word characters like the ",', etc. The length of word boundry is always "zero".

string = "hiox india"
"/bhiox" - Match boundry between double quotes, the word "hiox".
"/hioxb" - Match boundry between the word "hiox", the space.

PHP Example:

$name = "sam paul christ dany";
if (preg_match("/samb/", $name))
echo "Pattern matches!";
echo "Pattern not matched!";
Result :

Pattern matches!

In the above example the word "sam" and the space next to it is matched.

Perl Example:

#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$name= "sam-paul-christ-dany";
if ($name =~ m/sambpaul/)
{print "The name format matched!";}
{print "The name format not matched!";}
Result :

The name format matched!

In the above example the pattern "sambpaul" does not match since the word boundry is found for the word "sam" and "-". so when matching other characters "paul" specify non word character "-" too as "samb-paul" in the pattern that should match.

Ask Questions

Ask Question