V P
V P

Reputation: 35

Custom definition for word boundary for words that begin or end with non-word characters

I have an array of words that contain strings like "DOM" *".Net"* and "C++". I'm trying to perform whole word match for each of these strings in some text, by using the word boundary wild card. If the words are read into a variable, it would look like:

preg_match("/\b".preg_quote($word)."\b/",...)

This works fine for an example like "DOM", but not for ".Net" or "C++" because word boundary is also seen at . in case of .Net and is already seen at + in case of C++. Is there an alternative way in regular expressions in PHP to treat .Net or C++ as "words" for word boundary?

Upvotes: 0

Views: 157

Answers (2)

Nightfirecat
Nightfirecat

Reputation: 11610

This cannot be done, since \b matches for non-word characters (\W).

What you could do instead is search for characters that do not match some set of characters you define to be words, as shown below:

preg_match("/([^a-zA-Z_.+])".preg_quote($word)."\1/",...);

Edit: Added a backrefrence, so you only need to type that sequence once.

Upvotes: 2

Orangepill
Orangepill

Reputation: 24645

character classes... lets say you only want to do spaces and commas you would do this

preg_match("/[, ]".preg_quote($word)."[, ]/",...)

Upvotes: 0

Related Questions