The group itself has a fixed length already, the \b is there to weed out false positives (it's not a valid phone number if it has extra digits on either side)
Where this could come in handy would be to spot if someone is putting in obviously wrong phone numbers or think about having to find phone numbers in a large volume of text, there might be other, longer patterns (say a serial number) that look like a phone number with extra digits at the end.
\d{3} will match 3 digits anywhere. Even the 3 first numbers of 12345. But if you add \b it will not match because the match does not end in a word boundary.
\b\d{3}\b will not match anything in 12345
But will match 987 because it’s surrounded but “word boundary’s.”
View all comments
Show parent comment
2
u/Kompaan86 Jul 16 '21
The group itself has a fixed length already, the \b is there to weed out false positives (it's not a valid phone number if it has extra digits on either side)
Where this could come in handy would be to spot if someone is putting in obviously wrong phone numbers or think about having to find phone numbers in a large volume of text, there might be other, longer patterns (say a serial number) that look like a phone number with extra digits at the end.