正则表达式有效Twitter提及
发布时间:2020-12-14 06:26:24 所属栏目:百科 来源:网络整理
导读:我试图找到匹配的正则表达式,如果推文是真正的提及.值得一提的是,字符串不能以“@”开头,不能包含“RT”(不区分大小写),“@”必须以单词开头. 在示例中,我评论了所需的输出 一些例子: function search($strings,$regexp) { $regexp; foreach ($strings as $
我试图找到匹配的正则表达式,如果推文是真正的提及.值得一提的是,字符串不能以“@”开头,不能包含“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 编辑:
这是一个应该有效的正则表达式:
/^(?!.*bRTb)(?:.+s)?@w+/i 说明: /^ //start of the string (?!.*bRTb) //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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |