我正在尝试寻找一个正则表达式,以匹配推文是否是真正的提及。要成为提及,字符串不能以“@”开头,也不能包含“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
编辑:
我需要使用正则表达式,因为它可以在MySQL和其他语言中使用。我不是在寻找任何用户名,我只想知道字符串是否为提及。
RT
不区分大小写,例如在abort
的末尾使用rt
? - hakreEXTRACT_MENTIONS
模式可以在此处获得(https://github.com/twitter/twitter-text-java/blob/master/src/com/twitter/Regex.java)。 - Paul Calcraft