我有一个像“我爱我的国家”这样的字符串。如果我搜索“国家”,我的程序将显示“存在”。但是,如果我搜索“计数”,我的程序将显示“不存在”。
是这你在找什么?
我不是正则表达式的高手,所以有人可能有更优的解决方案,但只需搜索具有单词边界的国家/地区就可以得到您想要的东西。
/\b(country)\b/g
因此,在PHP中,如下所示:
$string = 'I love my country';
$matchExists = preg_match('/\b(country)\b/', $string);
echo ($matchExists) ? 'Match Found' : 'No Match Found'; // Output: Match Found
还有一个失败的:
$string = 'I love mycountry';
$matchExists = preg_match('/\b(country)\b/', $string);
echo ($matchExists) ? 'Match Found' : 'No Match Found'; // Output: No Match Found
并提醒您可以在这里看到一堆不同的输出,并测试您可能拥有的任何其他输出。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句