How to make preg_match to find whole word but not separate hyphen-words?

試著忘記壹切 提交于 2020-01-30 08:31:06

问题


if (preg_match('#\b'.$rawword.'\b#i',$body)) {   

This code finds whole words, but if they are a hyphen word like "ABLE-BODIED" it will find ABLE and BODIED separately. How can modify the expression to accommodate for the dash?


回答1:


You can use lookbehind and lookahead operators. This operators looks in behind and after but not match them.

for example use \b(?<!-)xyz(?!-)\b for finding whole words of xyz that doesn't have - before or after.



来源:https://stackoverflow.com/questions/5781451/how-to-make-preg-match-to-find-whole-word-but-not-separate-hyphen-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!