我很新的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";
}
括號再有就是從不爲第一個比賽正則表達式...
所以我堅持一個問題,使特定的正則表達式的工作。任何提示?提前謝謝了。
@ ghostdog74:我從來沒有想到看到一個襯墊的解決方案。哇,謝謝你的提示。我可以在這裏看到邏輯。 – 2010-04-08 14:56:05
Kool one-liner :) – Mike 2010-04-08 15:31:59