Regular expression for splitting on all unshielded half-columns

I use php preg_split to split a line based on half-columns, but I need it to be split into un-run half-columns.

<?
$str = "abc;def\\;abc;def";
$arr = preg_split("/;/", $str);
print_r($arr);
?>

It produces:

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

When I want it to create:

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

I tried "/(^\\)?;/"or "/[^\\]?;/", but both of them give rise to errors. Any ideas?

+5
source share
3 answers

It 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 use a negative lookbehind ( read about search ). Think of a “coincidence of all"; unless "1".

+5
source

PHP, :

/(?<!\\);/
+2

: , unescaped; escape-. :

<?
  $str = "abc;def\;abc\\\\;def";
  preg_match_all('/((?:[^\\\\;]|\\\.)*)(?:;|$)/', $str, $arr);
  print_r($arr);
?>

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

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

, "( , \;) (\ )" , a; .

, PHP $ end-of-line , , , .

0

All Articles