Lance
Lance

Reputation: 23

How to extract numbers enclosed in angle brackets in a string that might have numbers

I want to extract the numbers within angle brackets only in a string that can contain numbers as well.

I have a string $sig = wrp1<0:4> and i want to only extract the numbers enclosed in the angle brackets. I tried @range_nums=$sig=~/(\d+)/g; unfortunately this gives me 1 0 4...which means it also extracted the 1 in front of the wrp1 which is not what i want because it deletes all the non-digit characters...i wanted it to treat "wrp1" as a word.

my $sig = wrp1<0:4>;
@range_nums = $sig =~/(\d+)/g; ## extracts 1 0 4 but i want only 0 and 4. Please NOTE $sig can be also wrp<0:4> for example. key is to just extract the numbers in the angle bracket.

0 4 is expected

Upvotes: 0

Views: 121

Answers (1)

Dave Mitchell
Dave Mitchell

Reputation: 2403

Assuming there's only one range:

my $sig = 'wrp1<0:4>';
@range_nums = $sig =~/<(\d+):(\d+)>/;

Upvotes: 1

Related Questions