【问题标题】:Regex Valid Twitter Mention正则表达式有效的 Twitter 提及
【发布时间】: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


【解决方案1】:

这个正则表达式可能会更好一些:/\B\@([\w\-]+)/gim

这里有一个 jsFiddle 的例子:http://jsfiddle.net/2TQsx/96/

【讨论】:

  • @ 前面有空格或在后面的有效@mention 的字符串中找到杂散@ 时,似乎不处理两个。
  • @JoshHabdas 嗯,当我测试你列出的两个东西时,这个例子对我有用 - 如果你能重现,请告诉我。
  • 我责怪巴别塔。这是我 ended up using 基于此回复的内容。
【解决方案2】:

这是一个应该可以工作的正则表达式:

/^(?!.*\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.

【讨论】:

  • +1 用于解释,但您应该将 .*@ 更改为 .+@ 以验证您在 @ 之前至少有一个字符。
  • 成功了,非常感谢。但是对于带有电子邮件的新示例,它匹配并且不能:句子:Helo! ined@aser.com 你好吗!
  • @LDK 好的,我在@之前添加了一个空格字符检查
  • @Jacob 示例无效,但你给我一些想法:D /^(?!.*\bRT\b).+\s@([A-Za-z0-9_])/i
  • 当您在用户名之内或之后有点时,它也会停止工作,即@user.123,这在 Twitter 中是允许的。
【解决方案3】:

我发现这是在 javascript 中查找字符串内提及的最佳方法。我不知道我将如何做 RT,但我认为这可能有助于解决部分问题。

var str = "@jpotts18 what is up man? Are you hanging out with @kyle_clegg";
var pattern = /@[A-Za-z0-9_-]*/g;
str.match(pattern);
["@jpotts18", "@kyle_clegg"]

【讨论】:

    【解决方案4】:

    我想这样的事情会做到这一点:

    ^(?!.*?RT\s).+\s@\w+
    

    大致翻译为:

    在字符串的开头,向前看,发现 RT\s 不存在,然后找到一个或多个字符,后跟一个 @ 和至少一个字母、数字或下划线。

    【讨论】:

    • 现在它与电子邮件不匹配。
    【解决方案5】:

    Twitter 发布了他们在twitter-text 库中使用的正则表达式。他们还在 GitHub 上发布了其他语言版本。

    【讨论】:

    • 您的回答是我们不只发布链接的一个很好的理由。你应该在这里给我们正则表达式和链接。链接坏了,你的回答就没用了。
    • 我没有发布 232 行代码。这是一个完整的班级,而不是一个班轮。链接已更新,花了 5 秒才找到。
    • aaaa 又坏了。
    【解决方案6】:

    即使抓取工具有时会附加一些特殊字符,一个简单但也能正常工作:(?&lt;![\w])@[\S]*\b。这对我有用

    【讨论】:

    • 你为什么把\w\S放在字符类里?最后的\b没用,可能会导致匹配错误,你想用它做什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    相关资源
    最近更新 更多