开发者

Perl regex missing trailing parentheses on some, but not all records?

my @info = `net view printserver2`;
foreach my $printer (@info)
{
$printer =~ /.+\s+Print\s+\((.+)\)/;
print "$1\n";

gives me:

16-83
16-84) HP DesignJet 755CM(C3198A
16-84b
16-85
16-SW
17-80
18-45) HP DesignJet 250C (D/A1
18-51) HP DesignJet 650C(C2859B

This is the original:

(16-83) HP Designjet 800 42 by HP                              
(16-84) HP DesignJet 755CM(C3198A)by HP                        
(16-84b) HP Las开发者_开发技巧erJet 5100 Series PCL6                          
(16-85) HP Designjet T1100ps 44in HPGL2                        
(16-SW) HP LaserJet 4100 Series PCL6                           
(17-80) HP Color LaserJet 5500 PCL 6                           
(18-45) HP DesignJet 250C (D/A1) by HP                         
(18-51) HP DesignJet 650C(C2859B) by HP                        

What is wrong with my regular expression?

This is the result I want:

16-83
16-84
16-84b
16-85
16-SW
17-80
18-45
18-51


Your regex is matching up to the last ")" on the line. You need to specify a non-greedy match:

$printer =~ /.+\s+Print\s+\((.+?)\)/;

The question mark after .+ means to stop at the first opportunity.

Or, even better, specify that no ")" can be matched:

$printer =~ /.+\s+Print\s+\(([^)]+)\)/;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜