Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for spliting on all unescaped semi-colons

Tags:

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?

like image 753
Corey Hart Avatar asked Jan 20 '10 08:01

Corey Hart


2 Answers

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 '\'".

like image 138
nocksock Avatar answered Oct 05 '22 23:10

nocksock


I am not really proficient with PHP regexes, but try this one:

/(?<!\\);/
like image 28
Andrey Shchekin Avatar answered Oct 06 '22 00:10

Andrey Shchekin