ప్రధాన కంటెంట్‌కు వెళ్లండి
eLearner.app
మాడ్యూల్ 3 · 4లో పాఠం 2కోర్సులో 10/32~10 min
మాడ్యూల్ పాఠాలు (2/4)

పద సరిహద్దులు: `\b` `\B`

\b is a word boundary anchor: it matches the position between a word character (\w) and a non-word character (\W, or the start/end of the string). Like ^ and $, it does NOT consume characters.

Code
Pattern: \bgatto\b
Sample:  Il gatto e la gattina giocano.
            ^^^^^

gatto matches only as a whole word: inside gattina the sequence gatto is not there (the final o is missing), but it would not match gatti either because the final i is a word character.

\B is the opposite: it matches a position that is NOT a word boundary.

Finding "whole words"

The most typical use of \b is "match the word X only when isolated, not as part of another word":

Code
\bif\b   matches 'if' but not 'sniff', 'gift', 'lifetime'.

Word boundaries and non-word characters

The boundary \\b does not match any physical character; it is a position test. A \\b boundary exists between a \\w character and a non-\\w character (or start/end of text). The negation \\B asserts that the current position is not a word boundary.

Try it

వ్యాయామం#regex.m3.l2.e1
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Find every occurrence of the whole word `cat` (case-insensitive). It must NOT match `category`, `concatenate`, `scatter`.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Wrap 'cat' between two \\b: boundary at the start AND boundary at the end.

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Review exercise

వ్యాయామం#regex.m3.l2.e2
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Find every integer that is NOT part of an identifier (e.g. `42` yes, but neither `var42` nor `42abc`). Use `\\b` on both sides.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

\\b\\d+\\b matches only 'isolated' digit sequences. abc42 has 'c' (word) before the 42.

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Additional challenge

వ్యాయామం#regex.m3.l2.e3
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Find the sequence `cat` only if it starts a longer word, excluding when it appears as a whole word or at the end (e.g. match `catalog` but not `wildcat` or isolated `cat`).

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Use \b at the start of cat (word boundary) and \B at the end (non-word boundary).

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది