Regex Valid Twitter Mention

I am trying to find a regular expression that matches if there is a true mention in it. To be mentioned, a line cannot begin with “@” and cannot contain “RT” (case insensitive), and “@” must begin with a word.

In the examples, I commented on the desired result.

Some examples:

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 starting with @ it a reply "Helo!! :@ how are you!", // NO MATCH <- it 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 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 awesome?" // <- NO MATCH starting with @ it a reply and RT ); echo "Example 1:\n"; search($strings, "/(?:[[:space:]]|^)@/i"); 

Current output:

 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 awesome?" <- MATCH 

EDIT:

I need this in regex beacause, it can be used in MySQL and other languages ​​too. I am not looking for any usernames. I just want to know if the string is mentioned or not.

+8
source share
5 answers

There is a regex here:

 /^(?!.*\bRT\b)(?:.+\s) ?@ \w+/i 

Explanation:

 /^ //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. 
+6
source

This regex may work a little better: /\B\@([\w\-]+)/gim

Here is an example jsFiddle in action: http://jsfiddle.net/2TQsx/96/

+8
source

I think something like this will do it:

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

Roughly translated into:

At the beginning of the line, note that RT \ s is missing, then find one or more characters followed by @ and at least one letter, number, or underscore.

+1
source

I found this to be the best way to find mentions inside a string in javascript. I don’t know exactly how I will do RT, but I think this can help with part of the problem.

 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"] 
+1
source

Twitter has posted a regex that they use in their twitter-text library. There are other language versions on GitHub.

0
source

All Articles