正则表达式有效Twitter提及

前端之家收集整理的这篇文章主要介绍了正则表达式有效Twitter提及前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图找到匹配的正则表达式,如果推文是真正的提及.值得一提的是,字符串不能以“@”开头,不能包含“RT”(不区分大小写),“@”必须以单词开头.

在示例中,我评论了所需的输出

一些例子:

function search($strings,$regexp) {
    $regexp;
    foreach ($strings as $string) {
        echo "Sentence: \"$string\" <- " .
        (preg_match($regexp,$string) ? "MATCH" : "NO MATCH") . "\n";
    }
}

$strings = array(
"Hi @peter,I like your car ",// <- MATCH
"@peter I don't think so!",//<- NO MATCH: the string it's starting with @ it's a reply
"Helo!! :@ how are you!",// NO MATCH <- it's not a word,we need @(word) 
"Yes @peter i'll eat them this evening! RT @peter: hey @you,do you want your pancakes?",// <- NO MATCH "RT/rt" on the string,it's a RT
"Helo!! ineed@aser.com how are you!",//<- NO MATCH,it doesn't start with @
"@peter is the best friend you could imagine. RT @juliet: @you do you know if @peter it's awesome?" // <- NO MATCH starting with @ it's a reply and RT
);
echo "Example 1:\n";
search($strings,"/(?:[[:space:]]|^)@/i");

当前输出

Example 1:
Sentence: "Hi @peter,I like your car " <- MATCH
Sentence: "@peter I don't think so!" <- MATCH
Sentence: "Helo!! :@ how are you!" <- NO MATCH
Sentence: "Yes @peter i'll eat them this evening! RT @peter: hey @you,do you want your pancakes?" <- MATCH
Sentence: "Helo!! ineed@aser.com how are you!" <- MATCH
Sentence: "@peter is the best friend you could imagine. RT @juliet: @you do you know if @peter it's awesome?" <- MATCH

编辑:

I need it in regex beacause it can be used on MysqL and anothers
languages too. Im am not looking for any username. I only want to know
if the string it’s a mention or not.

这是一个应该有效的正则表达式:
/^(?!.*\bRT\b)(?:.+\s)?@\w+/i

说明:

/^             //start of the string
(?!.*\bRT\b)   //Verify that rt is not in the string.
(?:.*\s)?      //Find optional chars and whitespace the
                  //Note: (?: ) makes the group non-capturing.
@\w+           //Find @ followed by one or more word chars.
/i             //Make it case insensitive.
原文链接:https://www.f2er.com/regex/356982.html

猜你在找的正则表达式相关文章