0
processLine('23/05/2017 07:10:58 [6] 00-Always: ACTION=QUERY&Text=iphone%205%20has%20no%20network%2A&Summary=Context&SpellCheck=true&QuerySummary=false&Sort=AutnRank%2BRelevance&Synonym=true&TotalResults=true&MaxResults=10&PrintFields=drereference%2Cdretitle%2Ccontenttype%2Cautnrank%2COPTUS%5FFILTER1%2COPTUS%5FFILTER2%2COPTUS%5FFILTER3%2CCANONICAL%5FURL&Start=1&Predict=false&FieldText=%28MATCH%7BMy%20Optus%20Community%7D%3AOPTUS%5FFILTER1%3AOPTUS%5FFILTER2%3AOPTUS%5FFILTER3%20NOT%20MATCH%7Bsmb%7D%3ACONTEXT%20NOT%20MATCH%7BCustom%5FPromotions%7D%3ADREDBNAME%29%2BOR%2B%28%28MATCH%7BCustom%5FPromotions%7D%3ADREDBNAME%29%2BAND%2B%28BIASVAL%7Biphone%205%20has%20no%20network%2A%2C100%7D%3APromotion%5FKeywords%29%2BAND%2B%28MATCH%7Biphone%205%20has%20no%20network%2A%7D%3APromotion%5FKeywords%29%29&Combine=Simple&Characters=250 (127.0.0.1)');
if (defined $query && defined $ip && $query =~ m!/?a.*?=(\w+)([?&].*(?<=[?&])Text=([^?&]*))?!) 
{
        $events++;
        my $action = $1;
        my $terms = uri_unescape($3) || "";
}

我希望iphone%205%20has%20no%20network%2AText=iphone%205%20has%20no%20network%2A 中剥离并存储在$3. 我测试了正则表达式,似乎没有发现问题。它打印$3为上下文。

期望是$3输出值iphonehasnonetwork

当我经过时,

processLine('25/05/2017 14:48:10 [9] 00-Always: action=Query&text=samsung&databasematch=Help_Support&ResponseFormat=json&_=1495687690880 (127.0.0.1)');

它打印$3QuerySamsung. 这是预期的结果。

我是 Perl 的新手,我希望修改这个正则表达式并解决这个问题。我已经剥离了我的 perl 脚本并诊断出根本问题。在regex101.com中测试正则表达式的各个组件后,这个正则表达式对我来说看起来不错

4

2 回答 2

1

您忘记在您的正则表达式中添加忽略大小写修饰符:

$query =~ m!/?a.*?=(\w+)([?&].*(?<=[?&])Text=([^?&]*))?!i

i到底看到了什么?

在这里阅读更多:https ://perldoc.perl.org/perlre.html#Modifiers

于 2017-06-09T02:23:11.837 回答
0

我不确定你的正则表达式还有什么其他问题,但我马上看到:1)捕获组由左括号编号,所以我认为你想要 4 美元,而不是 3 美元 2)“文本”稍后可能匹配“字段文本”在字符串中

您真的应该通过拆分所有参数 (&) 然后拆分键值对 (=) 来正确解析 URI

于 2017-06-10T01:42:48.340 回答