fork download
  1. #!/usr/bin/perl
  2.  
  3. use 5.014;
  4. use strict;
  5. use warnings;
  6.  
  7. # Example of string setup
  8. my $str = qq{
  9. value1: [
  10.  
  11. 2018
  12. ],
  13. value2: [ 2019
  14. ],
  15. };
  16.  
  17. # Iterate over string and store desired values
  18. my $search_values = [1,2];
  19. my $matches = {};
  20. foreach my $number (@$search_values){
  21. if ($str =~ m/^value${number}[:\w ]*\[\s*?(\d+)\s*?\]/gm){
  22. $matches->{qq{value$number}} = $1;
  23. }
  24. }
  25.  
  26. # Example of result
  27. use Data::Dumper;
  28. say Dumper($matches);
  29.  
  30.  
  31.  
Success #stdin #stdout 0.02s 6036KB
stdin
Standard input is empty
stdout
$VAR1 = {
          'value2' => '2019',
          'value1' => '2018'
        };