简体   繁体   English

正则表达式:用特殊字符匹配单词

[英]Regex: Matching words with special characters

I'm trying to find a regular expression that matches a word in a string (the exact word). 我正在尝试找到一个正则表达式,该正则表达式与字符串中的一个单词(确切的单词)匹配。 The problem is when this word has special characters like '#' or anything else. 问题是该单词具有特殊字符(如“#”或其他任何字符)时。 The special characters could be any UTF-8 char, like ("áéíóúñ#@"), and it have to ignore punctuations marks. 特殊字符可以是任何UTF-8字符,例如(“áéíóúñ#@”),并且必须忽略标点符号。

I put some examples of what i'm looking for: 我举了一些我要找的例子:

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 Example code: PHP示例代码:

 $regexp= "#myword";
    if (preg_match("/(\w$regexp)/", "I like #myword!! It's awesome")) {
        echo "YES YES YES";
    } else {
        echo "NO NO NO ";
    }

Thank you! 谢谢!

Update: If I look for " myword " the word has to begin by "w" and not another char. 更新:如果我查找“ myword ”,则该单词必须以“ w”开头,而不是另一个字符。

Sentence: "I like myword!! It's awesome" <- MATCH
Sentence: "I like #myword It's awesome" <-NO MATCH

The solution below is produced when thinking about characters and boundaries separately. 当分别考虑字符和边界时,将产生以下解决方案。 There could also be a viable approach to use word boundaries directly. 也可能存在一种直接使用单词边界的可行方法。

Code: 码:

function search($strings,$search) {
        $regexp = "/(?:[[:space:]]|^)".$search."(?:[^\w]|$)/i";
        foreach ($strings as $string) {
                echo "Sentence: \"$string\" <- " . 
                     (preg_match($regexp,$string) ? "MATCH" : "NO MATCH") ."\n";
        }
}

$strings = array(
"I like the elephants when they say #myword",
"I like the elephants when they say #mywords",
"I like the elephants when they say myword",
"I don't like #mywords. its silly",
"I like #myword!! It's awesome",
"I like #mywOrd It's awesome",
);
echo "Example 1:\n";
search($strings,"#myword");

$strings = array(
"I like myword!! It's awesome",
"I like #myword It's awesome",
);
echo "Example 2:\n";
search($strings,"myword");

Output: 输出:

Example 1:
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
Example 2:
Sentence: "I like myword!! It's awesome" <- MATCH
Sentence: "I like #myword It's awesome" <- NO MATCH

You should search myword with wordboundary like this /\\bmyword\\b/ . 您应该使用// /\\bmyword\\b/这样的myword边界搜索myword
# itself is also a wordboundary so /\\b#myword\\b/ dosen't work. #本身也是一个单词边界,因此/\\b#myword\\b/不起作用。
one idea was to escape unicode character with \\X but this will create other problems. 一个想法是用\\X转义unicode字符,但这会带来其他问题。

/ #myword\b/

This should do the trick (replace "myword" with whatever you want to find): 这应该可以解决问题(将“ myword”替换为您要查找的任何内容):

^.*#myword[^\w].*$

If the match is a success then your word was found - otherwise it wasn't. 如果匹配成功,那么您找到了答案-否则没有找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM