【发布时间】:2011-02-19 03:46:07
【问题描述】:
新问题 Asaph 在上一个问题中提出的建议: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 中使用
【问题讨论】: