簡體   English   中英

正則表達式:用特殊字符匹配單詞

[英]Regex: Matching words with special characters

我正在嘗試找到一個正則表達式,該正則表達式與字符串中的一個單詞(確切的單詞)匹配。 問題是該單詞具有特殊字符(如“#”或其他任何字符)時。 特殊字符可以是任何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

當分別考慮字符和邊界時,將產生以下解決方案。 也可能存在一種直接使用單詞邊界的可行方法。

碼:

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");

輸出:

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

您應該使用// /\\bmyword\\b/這樣的myword邊界搜索myword
#本身也是一個單詞邊界,因此/\\b#myword\\b/不起作用。
一個想法是用\\X轉義unicode字符,但這會帶來其他問題。

/ #myword\b/

這應該可以解決問題(將“ myword”替換為您要查找的任何內容):

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

如果匹配成功,那么您找到了答案-否則沒有找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM