Question

I need help with a regular expression in PHP. I have one string containing a lot of data and the format could be like this.

key=value,e4354ahj\,=awet3,asdfa\=asdfa=23f23

So I have 2 delimiters , and = where , is the set of key and value. The thing is that key and value can contain the same symbols , and = but they will always be escaped. So I cant use explode. I need to use preg_split but I am no good at regular expressions.

Could someone give me a hand with this one?

Was it helpful?

Solution

You need to use negative lookbehind:

// 4 backslashes because they are in a PHP string, so PHP translates them to \\
// and then the regex engine translates the \\ to a literal \
$keyValuePairs = preg_split('/(?<!\\\\),/', $input);

This will split on every , that is not escaped, so you get key-value pairs. You can do the same for each pair to separate the key and value:

list($key, $value) = preg_split('/(?<!\\\\)=/', $pair);

See it in action.

OTHER TIPS

@Jon's answer is awesome. I though of providing a solution by matching the string:

preg_match_all('#(.*?)(?<!\\\\)=(.*?)(?:(?<!\\\\),|$)#', $string, $m);
// You'll find the keys in $m[1] and the values in $m[2]
$array = array_combine($m[1], $m[2]);
print_r($array);

Output:

Array
(
    [key] => value
    [e4354ahj\,] => awet3
    [asdfa\=asdfa] => 23f23
)

Explanation:

  • (.*?)(?<!\\\\)= : match anything and group it until = not preceded by \
  • (.*?)(?:(?<!\\\\),|$) : match anything and group it until , not preceded by \ or end of line.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top