LDK*_*LDK 9 regex twitter pattern-matching preg-match
我试图找到匹配的正则表达式,如果推文是真正的提及.值得一提的是,字符串不能以"@"开头,不能包含"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");
Run Code Online (Sandbox Code Playgroud)
当前输出:
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
Run Code Online (Sandbox Code Playgroud)
编辑:
我需要它在正则表达式,因为它也可以用于MySQL和其他语言.我不是在寻找任何用户名.我只想知道字符串是否提及.
这是一个应该有效的正则表达式:
/^(?!.*\bRT\b)(?:.+\s)?@\w+/i
Run Code Online (Sandbox Code Playgroud)
说明:
/^ //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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7618 次 |
| 最近记录: |