我试图找到字符串中的某个字(确切的词)相匹配的正则表达式。问题是这个词有什么特殊的字符,如'#'或其他。特殊字符可以是任何UTF-8字符,如(“áéíóúñ#@”),它必须忽略标点符号。正则表达式:匹配的词有特殊字符
我把我正在寻找一些例子:
Searching:#myword
Sentence: "I like the elephants when they say #myword" <- MATCH
Sentence: "I like the elephants when they say #mywords" <- NO MATCH
Sentence: "I like the elephants when they say myword" <-NO MATCH
Sentence: "I don't like #mywords. its silly" <- NO MATCH
Sentence: "I like #myword!! It's awesome" <- MATCH
Sentence: "I like #myword It's awesome" <- MATCH
PHP示例代码:
$regexp= "#myword";
if (preg_match("/(\w$regexp)/", "I like #myword!! It's awesome")) {
echo "YES YES YES";
} else {
echo "NO NO NO ";
}
谢谢!
更新:如果我找“myword”这个词有由“W”,而不是其他字符开始。
Sentence: "I like myword!! It's awesome" <- MATCH
Sentence: "I like #myword It's awesome" <-NO MATCH
如何在第二和第四产生一个匹配和不匹配? – alex 2011-03-21 13:47:08
它跟一个字母字符和4日就不是 – LDK 2011-03-21 13:51:57
尝试逃避与\ – Yaronius 2011-03-21 13:57:47