This is the sample text:
Item 1 : 1. A method of operating.
This matches 1.
:
\b1\.
But this does not match 1.
:
\b1\.\b
I need to have an exact match for 1.
I am testing it here.
This is the sample text:
Item 1 : 1. A method of operating.
This matches 1.
:
\b1\.
But this does not match 1.
:
\b1\.\b
I need to have an exact match for 1.
I am testing it here.
.
is not a word character. \b
is checking word boundaries, i.e. boundaries between word and characters not considered to be part of words. Therefore you cannot expect .
to be inside the "word" 1.
because these two characters do not form a word.
Quick reference document describes \b
as:
The match must occur on a boundary between a \w (alphanumeric) and a \W (nonalphanumeric) character.
And \w
is described as:
Matches any word character.
If you check what a Word character is, you will find it includes Unicode classes Ll [Letter, Lowercase]; Lu [Letter, Uppercase]; Lt [Letter, Titlecase]; Lo [Letter, Other]; Lm [Letter, Modifier]; Mn [Mark, Nonspacing]; Nd [Number, Decimal Digit] and Pc [Punctuation, Connector].
But .
has Unicode class Po [Punctuation, Other] which is not listed above.
So if you expect \b
to match a word boundary in 1.
, it is right between 1
and .
. This answers your question Why.
Note: .NET regex expressions should be preferably tested on testing sites dedicated to them like for example Regex Storm. If you test your regex using PCRE regex flavour (like on the site you linked), you can get different results from .NET.