use strict;
my $str = 'GET /v1/search?q=bob%20dylan&type=artist HTTP/1.1
Host: api.spotify.com
Cache-Control: no-cache
Postman-Token: e2f09f98-f';
my $regex = qr/(GET)\s(.+)\s(HTTP\/\d+\.\d+)\n(Host):\s(.+)$\n(Cache-Control):\s(.+)$\n(Postman-Token):\s(.+)$/mp;
my $subst = '{ headers: \\n\\t{ $4 \'$5\',\\n\\t \'$6\': \'$7\',\\n\\t \'$8\': \'$9\'\\n\\t}, \\n\\tverb: \'$1\',\\n\\tpath: \'$2\',\\n\\tprotocol: \'$3\'\\n}';
my $result = $str =~ s/$regex/$subst/rg;
print "The result of the substitution is' $result\n";
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html