Q:

Regex for spliting on all unescaped semi-colons

I'm using php's preg_split to split up a string based on semi-colons, but I need it to only split on non-escaped semi-colons.
<?
$str = "abc;def\\;abc;def";
$arr = preg_split("/;/", $str);
print_r($arr);
?>

Produces:
Array
(
    [0] => abc
    [1] => def\
    [2] => abc
    [3] => def
)

When I want it to produce:
Array
(
    [0] => abc
    [1] => def\;abc
    [2] => def
)

I've tried "/(^\\)?;/" or "/[^\\]?;/" but they both produce errors. Any ideas?

A:

This works.
<?
  $str = "abc;def\;abc;def";
  $arr = preg_split('/(?<!\\\);/', $str);
  print_r($arr);
?>

It outputs:
Array
(
    [0] => abc
    [1] => def\;abc
    [2] => def
) 

You need to make use of a negative lookbehind (read about lookarounds). Think of "match all ';' unless preceed by a '\'". 

