簡體   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