Consider the following string and regex:
$string= "just/RB convinced/VBN closing/VBG 10dma/NN need/VBN see/VB";
echo preg_replace("/(\w+)\/(JJ|RB|VB)/", "not$1/$2", $tweet);
I want to concatenate “not” to every word ending in /JJ, /RB or /VB. However, the regex also captures variations on /VB: /VBN and /VBG. The output is
notjust/RB notconvinced/VBN notclosing/VBG 10dma/NN notneed/VBN notsee/VB
However, the expected output is:
notjust/RB convinced/VBN closing/VBG 10dma/NN need/VBN notsee/VB
How can I stop the regex from grabbing there variations?
Use a
\bword boundary:\bonly matches between an alphanumeric character and either a non-alphanumeric character or the start/end of the string.