Perl: extracting multiple numbers from a string

Can someone help me in fixing me for the following code. I want to extract two numbers from the input string.

  input string [7:0] xxxx

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

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

I get the result as

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

considers

+4
source share
2 answers

[and ]are metacharacters of regular expressions, so you need to avoid them

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

Brackets are used to indicate a character class: [ ... ]that matches the characters inside it, for example. [abc]matches a.

+7
source

TLP . [] , . () [] * .., . .

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

, $max $min , .. [ 7: ] [ : ] [ : 2] [ ].

0

Source: https://habr.com/ru/post/1525549/


All Articles