Dcoder
Dcoder

Reputation: 379

How to parse through a string in perl to extract certain value?

I have following string

> 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

How to get BOX ID from above string in perl? The number of boxes here can vary . So based on the number of boxes "n", how to extract box Ids if the show box detail can go upto n nodes in the same format ?

Upvotes: 0

Views: 185

Answers (1)

ikegami
ikegami

Reputation: 385506

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

More restrictive:

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

Upvotes: 2

Related Questions