$x="[x=1 y=3 z=5]"; print join ":", $x=~/(\w+)=(\d+)/g; #### print join ":", $x=~/\G # start where prev match left off (?:\A\[)? # if start of string, expect [ (\w+)=(\d+) # our data (?:\s(?!\z) # between pairs, expect \s |\]\z) # or if end of string, expect ] /gcx; # if whole string was parsed, pos will be defined and equal to length print "error" unless pos($x) && pos($x) == length($x);