Regex Gültigen Twitter Erwähnen

Ich bin auf der Suche nach einem regex passt, wenn ein Tweet es ist eine wahre erwähnen. Ein schweigen, kann die Zeichenfolge nicht mit "@" beginnen und kann nicht enthalten, die mit "RT" (groß-und Kleinschreibung) und "@" beginnen müssen, das Wort.

In den Beispielen, die ich kommentierte die gewünschte Ausgabe

Einige Beispiele:

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!! [email protected] 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");

Aktuellen Ausgabe:

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!! [email protected] 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

EDIT:

Ich brauche es in regex beschreiben, weil es kann verwendet werden, um auf MySQL und anothers
Sprachen zu haben. Im bin nicht auf der Suche nach allen Benutzernamen. Ich will nur wissen
wenn die Zeichenfolge es ist eine Erwähnung oder nicht.

  • RT der groß-und Kleinschreibung, wie rt am Ende der Abbruch?
  • "rt" muss ein Wort sein, und es könnte sein, gefolgt von ":" Ex -: RT|rt|rt,:|RT,:|rT:| gültig sind
  • so abort: now ist eine gültige retweet?
  • Für alle, die sich für eine Allgemeine Muster zu finden, erwähnt, die EXTRACT_MENTIONS Muster verwendet, die von Twitter selbst zur Verfügung hier.
InformationsquelleAutor LDK | 2011-08-22
Schreibe einen Kommentar