Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想将字符串匹配为一个完整的单词匹配"~\b$search\b~i",它匹配'35'到'35-40'。我只希望空格作为单词之间的分隔符。
"~\b$search\b~i"
测试用例:在以下情况下匹配 35:
感谢您的回答
您可以使用环视断言而不是单词边界断言:
~(?<!\S)$search(?!\S)~i
这里(?<!\S)断言\S前面没有非空白字符 (),并(?!\S)断言后面没有非空白字符$search。
(?<!\S)
\S
(?!\S)
$search