0

This Regex Pattern: \bgoogle\b

Test case: http://www.google.com/

It matches.

Why?

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Ozgur
  • 1,744
  • 4
  • 17
  • 24

2 Answers2

3

Because a period represents a word boundary. That is, it is not part of a word, just as a space, a colon, a semicolon, or a tab character are not part of a word. \b is a zero-width assertion, meaning it does not match anything itself, it just defines something about the match.

Robusto
  • 31,447
  • 8
  • 56
  • 77
  • Is there any way to specify the word boundary characters ? (in my case it is space) – Ozgur Nov 11 '10 at 00:16
  • You can specify any characters you want. \s represents space characters. So \sgoogle\s will find the word "google" (lower case) surrounded by spaces. That may not be complete enough for all your needs, however, which is why \b may be good to stick with. Note that \bgoogle\b does not match the entire google URL, just the word google within it. – Robusto Nov 11 '10 at 00:20
1

Word boundaries with \b are very tricksy buggers indeed!

Community
  • 1
  • 1
tchrist
  • 78,834
  • 30
  • 123
  • 180