Reputation: 519
Let's say I have word phone
It's possible matches in my case are as follows
Cases to be Neglected [Here I'll mark the space with \s]
I've tried the following regex [^\w\s]items[^\w\s] link
But It didn't match the case of phone with no space in the beginning and the end as it requires 1 letter other than space and alphabets in the beginning and the end
Kindly suggest any other solutions which satisfies above mentioned cases
Upvotes: 2
Views: 2050
Reputation: 626747
You may use custom word boundaries, a combination of \b
and (?<!\S)
/ (?!\S)
:
(?<![\w\s])phone(?![\w\s])
See the regex demo and the regex graph:
The (?<![\w\s])
negative lookbehind pattern matches a location in string that is NOT immediately preceded with a word or whitespace char.
The (?![\w\s])
negative lookahead pattern matches a location in string that is NOT immediately preceded with a word or whitespace char.
Upvotes: 1