繁体   English   中英

Perl:获取与正则表达式错误匹配的子字符串

[英]Perl : get substring which matches regex error

我是Perl的新手,所以请忍受一个简单的问题:

这是示例输出:

Most successful agents in the Emarket climate are (in order of success):
1.  agent10896761       ($-8008)
2.  flightsandroomsonly     ($-10102)
3.  agent10479475hv     ($-10663)
Most successful agents in the Emarket climate are (in order of success):
1.  agent10896761       ($-7142)
2.  agent10479475hv     ($-8982)
3.  flightsandroomsonly     ($-9124)

我只对代理商名称及其对应的余额感兴趣,因此我希望得到以下输出:

agent10896761       -8008
flightsandroomsonly     -10102
agent10479475hv     -10663
agent10896761       -7142
agent10479475hv     -8982
flightsandroomsonly     -9124

对于以后的过程。

这是到目前为止我得到的代码:

#!/usr/bin/perl -w
open(MYINPUTFILE, $ARGV[0]);

while(<MYINPUTFILE>)
{
    my($line) = $_;
    chomp($line);

    # regex match test
    if($line =~ m/agent10479475/)
    {   
        if($line =~ m/($-[0-9]+)/)
        {
            print "$1\n";
        }

    }
    if($line =~ m/flightsandroomsonly/)
    {
        print "$line\n";
    }
}

第二个正则表达式匹配没有错,因为这将打印出整行。 但是,对于第一个正则表达式匹配,我得到了一些其他输出,例如:

$ ./compareResults.pl 3.txt
2.      flightsandroomsonly             ($-10102)
0479475
0479475
3.      flightsandroomsonly             ($-9124)
1.      flightsandroomsonly             ($-8053)
0479475
1.      flightsandroomsonly             ($-6126)
0479475

如果我像这样“逃脱”牙套

if($line =~ m/\($-[0-9]+\)/)
{
    print "$1\n";
}

那么第一个正则表达式永远不会匹配...

因此,我遇到了使该特定正则表达式工作的问题。 有什么提示吗? 提前谢谢了。

请记住,正则表达式中的$是字符串末尾的锚点。 对其进行转义以匹配字面的美元符号字符。

我会这样写:

#! /usr/bin/perl

use warnings;
use strict;

# for demo only
*ARGV = *DATA;

my $agent = qr/
  ^ \s* \d+ \.   # item number at the beginning of line
  \s+
  (\S+)          # agent name into $1
  \s+
  \( \s* \$ \s*  # start of balance
  (-?\d+)        # balance into $2
  \s* \)         # end of balance
  \s* $          # optional whitespace at the tail
/x;
while (<>) {
  if (my ($name,$balance) = /$agent/) {
    printf "%-20s : %d\n", $name, $balance;
  }
}

__DATA__
Most successful agents in the Emarket climate are (in order of success):
1.  agent10896761       ($-8008)
2.  flightsandroomsonly     ($-10102)
3.  agent10479475hv     ($-10663)
Most successful agents in the Emarket climate are (in order of success):
1.  agent10896761       ($-7142)
2.  agent10479475hv     ($-8982)
3.  flightsandroomsonly     ($-9124)

输出:

agent10896761        : -8008
flightsandroomsonly  : -10102
agent10479475hv      : -10663
agent10896761        : -7142
agent10479475hv      : -8982
flightsandroomsonly  : -9124

不要让*ARGV = *DATA行吓到您。 这使我可以在不更改处理逻辑的情况下将程序及其输入一起放在一个文件中。 在代码中,您将删除该行,然后以与以前相同的方式运行程序, 例如

$ ./compareResults.pl input.txt
perl -ane '$F[2]=~s/\(|\)//g;print "$F[1] $F[2]\n" if $F[1]=~/agent|flight/' file
use strict;
use warnings;

while(<DATA>){
    #split on whitespaces, pick 2nd and 3rd items
#check 2nd item matches pattern, do some trimming to 3rd
#store them to @data and print them
    my @data =grep{/\w{13,}/ || s/\(\$|\)//g;}((split' ')[1,2]);
    print join("\t",@data),"\n" if (@data);

}


__DATA__
1.  agent10896761       ($-8008)
2.  flightsandroomsonly     ($-10102)
3.  agent10479475hv     ($-10663)
Most successful agents in the Emarket climate are (in order of success):
1.  agent10896761       ($-7142)
2.  agent10479475hv     ($-8982)
3.  flightsandroomsonly     ($-9124)

__OUTPUT__
agent10896761   -8008
flightsandroomsonly     -10102
agent10479475hv -10663
agent10896761   -7142
agent10479475hv -8982
flightsandroomsonly     -9124

暂无
暂无

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

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