Regex Perl将字符剪切到旋转正则表达式,打印到行尾
我有这些数据,我想删掉日期,然后打印从首字母到结尾的所有内容。 我绘制了首字母缩写Regex Perl将字符剪切到旋转正则表达式,打印到行尾,regex,perl,cut,Regex,Perl,Cut,我有这些数据,我想删掉日期,然后打印从首字母到结尾的所有内容。 我绘制了首字母缩写 30th Mar 2020 5:53:18 pm Charlie Brown: BJ: Bloomberg Runs 30th Mar 2020 5:53:27 pm Charlie Brown: DS: ICE DATA = INC1018483661 30th Mar 2020 6:42:43 pm Boris Yeltsin: Cortese's ICE logs is for the Bloomberg
30th Mar 2020 5:53:18 pm Charlie Brown: BJ: Bloomberg Runs
30th Mar 2020 5:53:27 pm Charlie Brown: DS: ICE DATA = INC1018483661
30th Mar 2020 6:42:43 pm Boris Yeltsin: Cortese's ICE logs is for the Bloomberg Runs issue
30th Mar 2020 6:43:28 pm Charlie Brown: yeap
31st Mar 2020 4:11:22 am Ishtar Johnson: VK : RE: XS2018777099 & XS2018777172 - INC1018491954
31st Mar 2020 6:31:17 am Tommy Boy: NW: RE: SABSM 6.125 YTW - INC1018495843
31st Mar 2020 7:26:40 am Tommy Boy: AP: RE: Rolling 7yrs - INC1018497102
31st Mar 2020 7:45:36 am Tommy Boy: JK: RE: Chris White books - INC1018497380
这是代码-
#!/usr/bin/perl
use strict;
use warnings;
my @team = ("AP","II","DS","WJ", "JK","LC","BJ") ;
my ( $team_regex ) = map {qr /$_/} join "|", map {quotemeta} @team;
my @orderdTeam ;
my $filename = shift @ARGV ;
open(my $fh, '<', $filename) or die "Could not open file $filename $!";
while (my $line = <$fh> ) {
#$line =~ /($team_regex .*)/s ;
$line = /($team_regex .*)/s ;
print "$line\n";
}
close $fh;
这就是我要找的。“汤米男孩NW:”和“伊什塔约翰逊VK:”是我们球队的一部分,但来自欧洲。只会显示地图阵列“@team_regex”票证中的美国队成员。
时间和日期将从队列中删除
BJ: Bloomberg Runs
DS: ICE DATA = INC1018483661
AP: RE: Rolling 7yrs - INC1018497102
JK: RE: Chris White books - INC1018497380
第14行是这一行:
$line = /($team_regex .*)/s ;
匹配运算符(/…/
)使用=~
运算符处理绑定到它的变量,或者如果未给定此类变量,则使用$
。您没有使用=~
,因此匹配运算符尝试与$
进行匹配。而$\uuu
不包含任何数据,因此Perl会给您显示的“未定义值”警告
我认为您希望将正则表达式与$line
的内容相匹配。因此,您需要使用=~
而不是=
——就像在注释行中一样
$line =~ /($team_regex .*)/s ;
但在上面的一条评论中,您解释说您已将其注释掉,因为:
注释行不剪切任何字符-它打印整个行 当然,这是因为您没有编写任何代码来以任何方式更改
$line
。但是你想要的是比赛后的$1
,所以打印出来
$line =~ /($team_regex .*)/s ;
print $1;
但是像$1
这样的正则表达式变量只能在成功匹配时设置,因此在打印它们之前检查匹配是否有效很重要。您可以通过将匹配运算符放入if
语句中来实现这一点
if ($line =~ /($team_regex .*)/s) {
print $1;
}
更新:哦,这不起作用,因为数据中的团队代码后面跟一个冒号,而不是空格(正如正则表达式所假定的那样)。因此,将其更改为:
if ($line =~ /($team_regex:.*)/s) {
print $1;
}
请参阅下面的代码片段以演示如何实现所需的结果 我认为团队的正则表达式应该以不同的方式形成。跳过所有与正则表达式不匹配的记录。然后将前5个数据列替换为nothing并打印结果
use strict;
use warnings;
use feature 'say';
my @team = ("AP","II","DS","WJ", "JK","LC","BJ");
my $re_team = join ': |', @team;
my $filename = shift;
open(my $fh, '<', $filename)
or die "Could not open file $filename $!";
while( <$fh> ) {
chomp;
next unless /$re_team/;
s/^(\S+ ){5}//;
say;
}
close $fh;
输出
Charlie Brown: BJ: Bloomberg Runs
Charlie Brown: DS: ICE DATA = INC1018483661
Tommy Boy: AP: RE: Rolling 7yrs - INC1018497102
Tommy Boy: JK: RE: Chris White books - INC1018497380
BJ: Bloomberg Runs
DS: ICE DATA = INC1018483661
AP: RE: Rolling 7yrs - INC1018497102
JK: RE: Chris White books - INC1018497380
$VAR1 = [
'BJ: Bloomberg Runs',
'DS: ICE DATA = INC1018483661',
'AP: RE: Rolling 7yrs - INC1018497102',
'JK: RE: Chris White books - INC1018497380'
];
替换s/^(\s+{5}/代码>到s/^(\s+{7}/代码>以获得以下输出
BJ: Bloomberg Runs
DS: ICE DATA = INC1018483661
AP: RE: Rolling 7yrs - INC1018497102
JK: RE: Chris White books - INC1018497380
当然,代码可以写成
use strict;
use warnings;
use feature 'say';
my @team = ("AP","II","DS","WJ", "JK","LC","BJ");
my $re_team = join ': |', @team;
my $filename = shift;
open(my $fh, '<', $filename)
or die "Could not open file $filename $!";
/($re_team)/ && say /($1.*)/ while <$fh>;
close $fh
如果需要捕获数据
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
my @team = ("AP","II","DS","WJ", "JK","LC","BJ");
my $re_team = join ': |', @team;
my @data;
/($re_team)/ && push @data,/($1.*)/ while <>;
say Dumper(\@data);
注释行不剪切任何字符-它打印整个行
use strict;
use warnings;
use feature 'say';
my @team = ("AP","II","DS","WJ", "JK","LC","BJ");
my $re_team = join ': |', @team;
/($re_team)/ && say /($1.*)/ while <>;
BJ: Bloomberg Runs
DS: ICE DATA = INC1018483661
AP: RE: Rolling 7yrs - INC1018497102
JK: RE: Chris White books - INC1018497380
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
my @team = ("AP","II","DS","WJ", "JK","LC","BJ");
my $re_team = join ': |', @team;
my @data;
/($re_team)/ && push @data,/($1.*)/ while <>;
say Dumper(\@data);
$VAR1 = [
'BJ: Bloomberg Runs',
'DS: ICE DATA = INC1018483661',
'AP: RE: Rolling 7yrs - INC1018497102',
'JK: RE: Chris White books - INC1018497380'
];