【发布时间】:2011-11-01 07:32:13
【问题描述】:
我正在尝试找到一个匹配的正则表达式,如果 Tweet 它是一个真实的提及。需要说明的是,字符串不能以“@”开头,也不能包含“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不区分大小写,比如 rt 在 abort 的末尾? -
"rt" 必须是单词,后面可以跟":" 例如:RT|rt|rt:|RT:|rT:|有效
-
所以
abort: now是一个有效的转推? -
对于任何寻找通用模式来查找提及的人,Twitter 自己使用的
EXTRACT_MENTIONS模式是可用的here。
标签: regex twitter pattern-matching preg-match