2013-03-28 96 views
0

我有以下字符串如何解析perl中的字符串以提取某些值?

> show box detail 
    2 boxes: 
1) Box ID: 1 
    IP:     127.0.0.1 
    Interface:   1/1 
    Priority:   31 
2) Box ID: 2 
    IP:     192.68.1.1 
    Interface:   1/2 
    Priority:   31 

如何從在Perl上面的字符串BOX ID? 這裏的箱子數量可能會有所不同。因此,根據盒子的數量「n」,如果展示盒細節可以以相同格式上到n個節點,如何提取盒子ID?

回答

2
my @ids = $string =~ /Box ID: ([0-9]+)/g; 

更嚴格:

my @ids = $string =~ /^[0-9]+\) Box ID: ([0-9]+)$/mg; 
+0

能否請您解釋一下它是如何工作的? – Dcoder 2013-03-28 09:44:24

+0

列表上下文中的匹配運算符返回所有捕獲的字符串。如果使用/ g,這意味着從所有迭代中捕獲的所有字符串。簡而言之:它會返回所有的ID。 – ikegami 2013-03-28 10:13:30