Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: Extracting multiple numbers from string

Tags:

regex

perl

Could someone help me in correcting me for the following code. I want to extract the two numbers from a input string.

  input string [7:0] xxxx

I want '7' and '0' to be loaded into two variables (min and max). I am trying to achieve this by

my ($max, $min);
($max, $min) = $_ =~ /[(\d+):(\d+)]/;
print "min: $min max $max\n";

I am getting a result as

Use of uninitialized value in concatenation (.) or string at constraints.pl line 16, <PH> line 165.
min:  max: 1

regards

like image 639
user1495523 Avatar asked Mar 20 '23 16:03

user1495523


1 Answers

[ and ] are regex meta characters, so you have to escape them

($max, $min) = $_ =~ /\[(\d+):(\d+)\]/;

The brackets are used to denote a character class: [ ... ] which matches the characters inside it, e.g. [abc] matches a.

like image 76
TLP Avatar answered Mar 29 '23 15:03

TLP