如何让 preg_match 找到整个单词而不是单独的连字符? [英] How to make preg_match to find whole word but not separate hyphen-words?

查看:56
本文介绍了如何让 preg_match 找到整个单词而不是单独的连字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

此代码查找整个单词,但如果它们是像ABLE-BODIED"这样的连字符单词,它将分别查找 ABLE 和 BODIED.如何修改表达式以适应破折号?

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?

推荐答案

您可以使用lookbehind 和lookahead 运算符.此运算符在前后查找但不匹配.

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

例如使用 \b(?<!-)xyz(?!-)\b 来查找 xyz 没有 的整个单词>- 之前或之后.

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

这篇关于如何让 preg_match 找到整个单词而不是单独的连字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆