my $string1 = "one=1,two=2,three=3,four=4,five=5"; my $string2 = "one=1,three=3,five=5"; $string1 =~ m/ (?:one=(.*?),)? .* (?:two=(.*?),)? .* (?:three=(.*?),)? .* (?:four=(.*?),)? .* (?:five=(.*?),)? /x; print "one: $1, two: $2, three: $3, four: $4, five: $5\n";