use Regexp::Grammars; my $parser = qr{ (?: <[Hash]> ** ( , ) ) \{ <[KV]> ** ( , ) \} (?: \s* <[Key]> \s* => \s* <[Value]> \s* ) \w* ' \w* ' }x; use Data::Dumper; # Match the grammar against some text... if ("{ foo => 'bar', baz => 'bof' }, {bing => 'go'}" =~ $parser) { # If successful, the hash %/ will have the hierarchy of results... print Dumper(\%/ ); } __END__ $VAR1 = { '' => '{ foo => \'bar\', baz => \'bof\' }, {bing => \'go\'}', 'Hash' => [ { '' => '{ foo => \'bar\', baz => \'bof\' }', 'KV' => [ { '' => ' foo => \'bar\'', 'Value' => [ '\'bar\'' ], 'Key' => [ 'foo' ] }, { '' => 'baz => \'bof\' ', 'Value' => [ '\'bof\'' ], 'Key' => [ 'baz' ] } ] }, { '' => '{bing => \'go\'}', 'KV' => [ { '' => 'bing => \'go\'', 'Value' => [ '\'go\'' ], 'Key' => [ 'bing' ] } ] } ] };