ADVERTISEMENT
ADVERTISEMENT

What is a Word Boundary in Regex?

A word boundary in regex, written as \b, is a marker that matches the position between a word character (letters, numbers, or underscore) and a non-word character (space, punctuation, or start/end of a string). It helps to find whole words without matching parts inside other words.It ensures precise word searches in text.—it just asserts a boundary.

Why Use Word Boundaries?

Word boundaries help match whole words without partial matches. Without \b, regex might match substrings inside larger words. With \b, you ensure precise matches.

Common Scenarios:

  • Match exact words in a text.
  • Avoid partial matches inside larger words.
  • Detect words at the start or end of strings.

Examples:

<?php
$text1 = "The bat flew into the bathtub.";
$text2 = "bat is sleeping.";
$text3 = "He has a cat.";
$text4 = "catalog of categories";
$text5 = "batman and battle are heroes.";
$text6 = "Hello, world! Welcome to the world.";
$text7 = "cat";

// 1. Match Whole Word Only
echo "1. Whole word match:\n";
echo preg_match('/\bbat\b/', $text1) ? "Found 'bat'\n" : "Not Found\n";  
// Output: Found 'bat'

// 2. Match Word at the Start
echo "\n2. Word at the start:\n";
echo preg_match('/\bbat/', $text2) ? "Word starts with 'bat'\n" : "Not Found\n";
// Output: Word starts with 'bat'

// 3. Match Word at the End
echo "\n3. Word at the end:\n";
echo preg_match('/cat\b/', $text3) ? "Word ends with 'cat'\n" : "Not Found\n";
// Output: Word ends with 'cat'

// 4. No Match Without Boundary
echo "\n4. No match without boundary:\n";
echo preg_match('/\bcat\b/', $text4) ? "Found 'cat'\n" : "Not Found\n";
// Output: Not Found

// 5. Using Non-Word Boundary (\B)
echo "\n5. Non-word boundary match:\n";
if (preg_match_all('/\Bbat/', $text5, $matches5)) {
    print_r($matches5[0]);
}
// Output: Array ( [0] => bat [1] => bat )

// 6. Match Words Surrounded by Spaces or Punctuation
echo "\n6. Word match with punctuation:\n";
if (preg_match_all('/\bworld\b/', $text6, $matches6)) {
    print_r($matches6[0]);
}
// Output: Array ( [0] => world [1] => world )

// 7. Boundary at Start and End of String
echo "\n7. Exact match for full string:\n";
echo preg_match('/\bcat\b/', $text7) ? "Exact match for 'cat'\n" : "Not Found\n";
// Output: Exact match for 'cat'
?>

 


ADVERTISEMENT

ADVERTISEMENT