v0d1ch
v0d1ch

Reputation: 2748

Regexp boundary and string 'c#'

Just curious to know. I noticed that my regexp can't find string 'C#' when using it with '\b'

if (preg_match('@\bC#\b@i', $userSkills)) {...

However if I remove '\b' it manages to find string.

Does anybody know what might be the problem here ?

String to find matches:

Unified Communications Design Specialist ~$~ Windows 95 or 98 ~$~ Windows 2000 
or XP ~$~ Linux ~$~ Visual Basic ~$~ ASP Net ~$~ C# ~$~ Contact Center Design 
Specialist ~$~ Internet Programming ~$~ Object oriented programming ~$~ 
Databases ~$~ Unified Commucations ~$~ Contact Center Solutions ~$~
Junior Developer ~$~ Solutions Engineer 

Upvotes: 1

Views: 92

Answers (2)

Fabian Schmengler
Fabian Schmengler

Reputation: 24576

It's because # is no word character. I marked the word boundaries in your string around "C#" with |:

|ASP| |Net| ~$~ |C|# ~$~ |Contact|

You see, between "#" and "" there is no boundary, so \b does not match.

Upvotes: 1

nickb
nickb

Reputation: 59709

The escape sequence is \b not /b for word boundaries.

if (preg_match('@\bC#\b@i', $userSkills)) {...

Upvotes: 2

Related Questions