perl - 使用 Perl 解析 Symphony 聊天

标签 perl

我们在工作中从 MindAlign 切换到 Symphony Chat。 Symphony 似乎类似于 ZenDesk 聊天软件。我们使用 Symphony Chat 向人们分配工单。

当我从 Symphony 终端剪切和粘贴时,结果如下(没有换行符 - 它只是一大行连续的文本):

16th Jun 2020 7:57:18 am Tom Lewin: WJ: RE: BART failed STP - JIMBCI - INC101981467816th Jun 2020 8:20:38 am Nathan Winslow: II : RE: Loans are experiencing issues sending RUNZ - INC101981521816th Jun 2020 8:57:58 am Nathan Winslow: NW : RE: FW: Missing pool factor PnL [Restricted - Internal] - INC101981603016th Jun 2020 9:13:49 am Nathan Winslow: JK : RE: missing sales credits - INC101981633816th Jun 2020 9:24:26 am Nathan Winslow: KB : RE: Bookbuilder not responding - INC1019816567

所以我写了这个脚本来更好地构建它 - 它工作正常。

#!/usr/bin/perl
use warnings ;
use strict ;

my $filename = shift @ARGV ;
open(my $fh, '<', $filename) or die "Could not open file 
$filename $!";
while (my $line = <$fh>) {

chomp $line ;
my @words = split /(\d{2}(?:st|nd|rd|th) \w{3} \d{4})/a, $line;

foreach my $word(@words) {
        if($word =~ /\d{2}(?:st|nd|rd|th) \w{3} \d{4}/a) {
        chomp $word ;
        print $word ;
        }
        else {  print "$word\n"; }
    }
}

这是输出。这很好——但我发现自己花了太多时间清理数据。

16th Jun 2020 7:57:18 am Tom Lewin: WJ: RE: BART failed STP - JIMBCI - INC1019814678
16th Jun 2020 8:20:38 am Nathan Winslow: II : RE: Loans are experiencing issues sending RUNZ - INC1019815218
16th Jun 2020 8:57:58 am Nathan Winslow: NW : RE: FW: Missing pool factor PnL [Restricted - Internal] - INC1019816030
16th Jun 2020 9:13:49 am Nathan Winslow: JK : RE: missing sales credits - INC1019816338   
16th Jun 2020 9:24:26 am Nathan Winslow: KB : RE: Bookbuilder not responding - INC1019816567

这就是问题所在——这是我需要的输出:

06/16/2020 WJ: RE: BART failed STP - JIMBCI - INC1019814678
06/16/2020 II : RE: Loans are experiencing issues sending RUNZ - INC1019815218 
06/16/2020 NW : RE: FW: Missing pool factor PnL - INC1019816030
06/16/2020 JK : RE: missing sales credits - INC1019816338
06/16/2020 KB : RE: Bookbuilder not responding - INC1019816567

我试过正则表达式和分割线,但它只是一团糟。 WJ NW JK KB II 那些是首字母 - 它们是不变的。有时他们在冒号 (:) 后有一个空格,有时则没有。但是,我只需要日期和数据,从首字母开始到票号 INC00000000 的最后一位结束。

最佳答案

如果您有明确的 anchor ,一种提取所需内容的方法是正则表达式

my @parts = $string =~ /
    ([0-9]+)             # numbers for day
    (?:[^0-9]+)?\s+      # for st|nd|rd|th (optional!), space. not captured
    (\w+)\s+             # month
    ([0-9]+)\s+          # year
    .+?                  # the rest, but only up to the initials 
    ( (?:WJ|NW|JK|KB|II): .+? INC[0-9]+ )
/x; 

这里的某些模式可以加强或削弱(例如,我们可以使用 [A-Z]+:,而不是预期首字母的交替,允许其他模式和更多字母)。

然后将时间转换为所需的时间戳。一个很好的工具是 Time::Piece .一共

use warnings;
use strict;
use feature 'say';

use Time::Piece;

my $string = q(16th Jun 2020 7:57:18 am Tom Lewin: WJ: RE: BART failed STP - JIMBCI - INC101981467816th Jun 2020 8:20:38 am Nathan Winslow: II : RE: Loans are experiencing issues sending RUNZ - INC101981521816th Jun 2020 8:57:58 am Nathan Winslow: NW : RE: FW: Missing pool factor PnL [Restricted - Internal] - INC101981603016th Jun 2020 9:13:49 am Nathan Winslow: JK : RE: missing sales credits - INC101981633816th Jun 2020 9:24:26 am Nathan Winslow: KB : RE: Bookbuilder not responding - INC1019816567);

my @parts = $string =~ /
    ([0-9]+) (?:[^0-9]+)?\s+ (\w+)\s+ (\w+)\s+ .+? 
    ( (?:WJ|NW|JK|KB|II): .+? INC[0-9]+ )
/x;
#say for @parts;  say '---';

my $dt = Time::Piece->strptime("@parts[0..2]", "%d %b %Y");

say $dt->mdy('/'), ' ', $parts[3];

最后一点也许做得更好

my $date = Time::Piece
    -> strptime( join(' ', splice @parts, 0, 3), "%d %b %Y")
    -> mdy('/');

say "$date @parts";

现在我们不必计算要打印的元素的确切数量。

在这种情况下,@parts 最终只有一个元素,但需求确实发生了变化。此外,如果某些元素实际上需要作为其他目的单独使用(添加捕获括号集),则 @parts 将包含更多元素。

这些打印需要的东西。

关于perl - 使用 Perl 解析 Symphony 聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62421099/

相关文章:

perl - 如何通过 Excel 中的 ActiveX 控件运行 Perl 脚本?

Perl LWP::UserAgent,特定 https 网站的 https 代理,未知协议(protocol)错误

perl - 绑定(bind)标量的构造函数

perl - 使用 JSON-RPC 协议(protocol)的 Perl Client to API 使用哪些模块?

Perl 脚本适用于 5.8.4 和 5.12.4 但不适用于 5.10.0

Perl 正则表达式摘录

regex - s///返回错误的换行符

perl - 将数组传递给 Perl sub 时出现 "Too many arguments"?

perl - 如何打印匹配的行,紧靠其上方的一行和紧邻其下方的一行?

perl - 为什么我的 Perl 测试错误地失败了?