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

时间:2010-04-08 14:44:30

标签: regex perl

我对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";
}

然后第一个正则表达式永远不会匹配...

所以我遇到了使特定正则表达式工作的问题。有什么提示吗?非常感谢提前。

3 个答案:

答案 0 :(得分:4)

请记住,正则表达式中的$是字符串末尾的锚点。将其转义为与文字美元符号字符匹配。

我会这样写:

#! /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

答案 1 :(得分:3)

perl -ane '$F[2]=~s/\(|\)//g;print "$F[1] $F[2]\n" if $F[1]=~/agent|flight/' file

答案 2 :(得分:1)

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