0

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 ?

1 Answer 1

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

More restrictive:

my @ids = $string =~ /^[0-9]+\) Box ID: ([0-9]+)$/mg;
Sign up to request clarification or add additional context in comments.

1 Comment

The match operator in list context returns all captured strings. If /g is used, this means all captured string from all iterations. In short: it returns all the ids.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.