简体   繁体   English

需要PHP正则表达式来从主题行解析Twitter名称

[英]Need PHP regex to parse Twitter name from subject line

I'm using PHP and trying to parse this subject line to isolate just the twitter name (@MargyM): 我正在使用PHP并尝试解析此主题行以仅隔离Twitter名称(@MargyM):

Margaret M (@MargyM) is now following you on Twitter

Note that the user name (before the twitter name) can be something else, with parens, so the regex needs to be smart enough to work around that-- ie any of these could be valid inputs: 请注意,用户名(在twitter名称之前)可以是其他名称,并带有括号,因此正则表达式必须足够聪明以解决该问题-即以下任何一个都是有效输入:

Margaret M (:) (@MargyM) is now following you on Twitter
Margaret M ):) (@MargyM) is now following you on Twitter
Margaret M. :) (@MargyM) is now following you on Twitter
Margaret M (-- (@MargyM) is now following you on Twitter
Margaret M ( : (@MargyM) is now following you on Twitter
Margaret M (_: (@MargyM) is now following you on Twitter

So to be clear, the RegEx should look at the bit of text between the final set of parens before "is now following you..." to get the Twitter name. 为了清楚起见,RegEx应该在“现在跟随您...”之前查看最后一组paren之间的文本位,以获取Twitter名称。

EDIT: it's also possible that one or more "@" signs could appear before the twitter name, ie anything like this: 编辑:也可能一个或多个“ @”符号可能出现在Twitter名称之前,即像这样的东西:

Margaret @ Minnesota (@MargyM) is now following you on Twitter
Margaret @ Minnesota :-) (@MargyM) is now following you on Twitter
Margaret @ Minnesota (-) (@MargyM) is now following you on Twitter
Margaret @ Minnesota (tweet @ME!) (@MargyM) is now following you on Twitter
etc.

This? 这个?

if (preg_match('/(?<=@).*(?=\))/', $subject, $regs)) {
    #$regs[0];
}

After your edit : 编辑后:

if (preg_match('/(?<=@)(?!.*@).*(?=\))/', $subject, $regs)) {
    #$regs[0];
}

Explanation: 说明:

"
(?<=     # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
   @        # Match the character “@” literally
)
(?!      # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
   .        # Match any single character that is not a line break character
      *        # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   @        # Match the character “@” literally
)
.        # Match any single character that is not a line break character
   *        # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?=      # Assert that the regex below can be matched, starting at this position (positive lookahead)
   \)       # Match the character “)” literally
)
"
$regex = "/(\(@.+\))/i";
preg_match( $regex , $input , $match );

A very simple solution: 一个非常简单的解决方案:

/\((@[^)]+)\) is now following you on Twitter/

Or alternatively, if the following text isn't always like that but never contains any @ itself: 或者,如果以下文本并非总是这样,但绝不包含任何@本身:

/\((@[^)]+)\)(?:[^@]+)$/m

You can see a working example of the first here and of the second here . 你可以看到第一个的工作示例这里和第二的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM