如何使 preg_match 查找整个单词而不是单独的连字符单词?
if (preg_match('#\b'.$rawword.'\b#i',$body)) {
此代码查找整个单词,但如果它们是像“ABLE-BODIED”这样的连字符单词,它将分别查找 ABLE 和 BODIED。如何修改表达式以适应破折号?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用lookbehind 和lookahead 运算符。该运算符查看后面和后面但不匹配它们。
例如,使用
\b(? 查找不包含
-< 的
xyz
的整个单词/code> 之前或之后。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 ofxyz
that doesn't have-
before or after.