|
|
Word Boundry "\b" metacharacter in Regular Expression.
|
Tutorials

Regular-expression

|
Topic |
What is Word Boundry?
|
|
Explanation |
|
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".
"/hiox\b" - Match boundry between the word "hiox", the space.
PHP Example:
<?php
$name = "sam paul christ dany";
if (preg_match("/sam\b/", $name))
echo "Pattern matches!";
else
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:\programfiles\perl\bin\perl
print "content-type: text/html\n\n";
$name= "sam-paul-christ-dany";
if ($name =~ m/sam\bpaul/)
{print "The name format matched!";}
else
{print "The name format not matched!";}
Result:
The name format matched!
In the above example the pattern "sam\bpaul" 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 "sam\b-paul" in the
pattern that should match.
|
|
|
|