Parsing a string into a hash

I have a line:

<https://gitlab.me.com/api/v3/projects/all?page=2&per_page=5>;
rel="next",
<https://gitlab.me.com/api/v3/projects/all?page=1&per_page=5>;
rel="first",
<https://gitlab.me.com/api/v3/projects/all?page=8&per_page=5>;
rel="last"

Thus format

(<val>; rel="key")*

And I want to parse this hash in the following format:

next => https://gitlab.me.com/api/v3/projects/all?page=2&per_page=5
first => https://gitlab.me.com/api/v3/projects/all?page=1&per_page=5
last => https://gitlab.me.com/api/v3/projects/all?page=8&per_page=5

In Java, I would use a regular expression pattern to extract each key pair => value and put them on a map. The template will look something like this:

<([^>]++)>;\s*rel="([^"]++)"

Which would give me the key in the second group of matches and the value in the first. Would the same approach be the best way to achieve this - is it Perl, or is there something eye-popping that I could do?

PS The reason I use Perl, not Java, is because there is no Java on the server.

+4
source share
3 answers

split ",", map :

#!/usr/bin/env perl

use strict;
use warnings;

my $str = 'https://gitlab.me.com/api/v3/projects/all?page=2&per_page=5; rel="next", https://gitlab.me.com/api/v3/projects/all?page=1&per_page=5; rel="first", https://gitlab.me.com/api/v3/projects/all?page=8&per_page=5; rel="last"';

my %hash = map { 
    my ($v, $k) = $_ =~ /\s*([^;]+);\s*rel="([^"]+)".*/; 
    $k => $v;
} split ',', $str;

foreach my $key (keys %hash) {
    print "$key => $hash{$key}\n"
}

:

first => https://gitlab.me.com/api/v3/projects/all?page=1&per_page=5
next => https://gitlab.me.com/api/v3/projects/all?page=2&per_page=5
last => https://gitlab.me.com/api/v3/projects/all?page=8&per_page=5

:

$str = q(<https://gitlab.me.com/api/v3/projects/all?page=2&per_page=5>; rel="next", <https://gitlab.me.com/api/v3/projects/all?page=1&per_page=5>; rel="first", <https://gitlab.me.com/api/v3/projects/all?page=8&per_page=5>; rel="last");

my %hash = map { 
    my ($v, $k) = $_ =~ /<([^>]+)>;\s*rel="([^"]+)".*/; 
    $k => $v;
} split ',', $str;

.

+4

, , , , while.

, . (Perl, , , !)

, markdown . ? , ?

use strict;
use warnings;

my $str = <<'END';
<https://gitlab.me.com/api/v3/projects/all?page=2&per_page=5>;
rel="next",
<https://gitlab.me.com/api/v3/projects/all?page=1&per_page=5>;
rel="first",
<https://gitlab.me.com/api/v3/projects/all?page=8&per_page=5>;
rel="last"
END

my %data;
while ($str =~ / < ([^<>]+) >; \s* rel="([^"]+)" (?:,\s*)? /xg) {
  $data{$2} = $1;
}

use Data::Dump;
dd \%data;

{
  first => "https://gitlab.me.com/api/v3/projects/all?page=1&per_page=5",
  last  => "https://gitlab.me.com/api/v3/projects/all?page=8&per_page=5",
  next  => "https://gitlab.me.com/api/v3/projects/all?page=2&per_page=5",
}
+6
use strict;
use warnings;
my $string='https://gitlab.me.com/api/v3/projects/all?page=2&per_page=5; rel="next", https://gitlab.me.com/api/v3/projects/all?page=1&per_page=5; rel="first", https://gitlab.me.com/api/v3/projects/all?page=8&per_page=5; rel="last"';

my @array=split /,/, $string;
my %hash;

foreach(@array)
{
   if($_=~/(.*?);\s*rel\=\s*"([^"]+)"/)
   {
      $hash{$2}=$1;
   }
}

print "$_ =>  $hash{$_}\n" foreach(keys%hash);
+1
source

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


All Articles