新问题正如 Asaph 在上一个问题中所建议的:正则表达式检查确切的字符串是否存在 https://stackoverflow.com/questions/2824291/regex-to-check-if-exact-string-exists
我正在寻找一种方法来使用正则表达式或建议的任何更好的方法来检查另一个字符串中是否存在精确的字符串匹配。据我所知,您告诉正则表达式在字符串的开头或结尾匹配空格或任何其他非单词字符。但是,我不知道具体如何设置。
搜索字符串:#t
应匹配:
字符串 1:Hello World, Nice to see you! #t
字符串2:#T Hello World, Nice to see you!
字符串 3:Hello World, #t Nice to see you!
不应匹配:
字符串 1:Hello World, Nice to see you!
字符串2:Hello World, Nice to see you! #ta
字符串 3:#tHello World, Nice to see you!
编辑2:添加更多字符串样本
编辑 Serg555 和 SilentGhost 1:
搜索字符串中允许的字符:
#[_a-zA-Z0-9]
#
是可选的。
要求:
搜索字符串可以位于主题中的任何字符位置。
它之前或之后可能有也可能没有空白字符。
如果它是另一个字符串的一部分,我不希望它匹配;例如单词的一部分。
为了这个问题:我想我会使用这种模式来做到这一点:/\b\#t\b/gi
然而,这并没有像我预期的那样返回结果。
我可以使用以下方法找到正常字符串(不存在 # 的字符串)的精确匹配:
/\b{$search_string}\b/gi
附加信息:这将在 PHP 5 中使用