There is no need to do this with regex. You can solve it easily by splitting the string twice based on some rules.
Update: Here's the php code. Will work without a single regular expression, also for more complex combinations of these properties.
function split_string($string) {
$properties = Array();
$pairs = explode('^', $string);
while ($pairs) {
$first_pair = split('#', array_shift($pairs));
if ($first_pair[0] == 'Name') {
$second_pair = split('#', array_shift($pairs));
$properties[$first_pair[1]] = $second_pair[1];
} else {
$properties[$first_pair[0]] = $first_pair[1];
}
}
return $properties;
}
print_r(split_string("Name#Size^Value#S^Name#Color^Value#Black^Quantity#2"));
print_r(split_string("Name#Size^Value#XXL^Quantity#3"));
Output:
Array
(
[Size] => S
[Color] => Black
[Quantity] => 2
)
Array
(
[Size] => XXL
[Quantity] => 3
)