Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex word boundary alternative

I was using the standard \b word boundary. However, it doesn't quite deal with the dot (.) character the way I want it to.

So the following regex:

\b(\w+)\b

will match cats and dogs in cats.dog if I have a string that says cats and dogs don't make cats.dogs.

I need a word boundary alternative that will match a whole word only if:

  1. it does not contain the dot(.) character
  2. it is encapsulated by at least one space( ) character on each side

Any ideas?!

P.S. I need this for PHP

like image 916
ObiHill Avatar asked Dec 28 '12 18:12

ObiHill


1 Answers

You could try using (?<=\s) before and (?=\s) after in place of the \b to ensure that there is a space before and after it, however you might want to also allow for the possibility of being at the start or end of the string with (?<=\s|^) and (?=\s|$)

This will automatically exclude "words" with a . in them, but it would also exclude a word at the end of a sentence since there is no space between it and the full stop.

like image 200
Niet the Dark Absol Avatar answered Oct 04 '22 20:10

Niet the Dark Absol