如何编写分割字符串但忽略分隔符的转义序列的正则表达式?

时间:2013-09-06 16:51:57

标签: php regex split escaping

我希望使用PHP的mb_split拆分冒号分隔的字符串,但忽略转义的冒号(即\:)。

例如:

Part A is here:Part B\: is over here:Part C\ is here:Part \\:D is here

需要分成:

  1. Part A is here
  2. Part B: is over here
  3. Part C\ is here
  4. Part \:D is here
  5. 我不确定如何解决这个问题。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您可以尝试拆分:

(?<!\\):

(?<!\\)是一个负面的背后隐藏,如果:之前的\不匹配,则会使其不匹配。

[注意我从未尝试mb_split,但这应该适用于preg_split]

答案 1 :(得分:1)

这是我搜索者参考的完整解决方案。

<?php
if (!function_exists('mb_str_replace')) {
    function mb_str_replace ($needle, $replacement, $haystack) {
        // Courtesy of marc at ermshaus dot org: http://www.php.net/manual/en/ref.mbstring.php#86120
        $needleLength = mb_strlen($needle);
        $replacementLength = mb_strlen($replacement);
        $pos = mb_strpos($haystack, $needle);
        while ($pos !== false) {
            $haystack = mb_substr($haystack, 0, $pos).$replacement.mb_substr($haystack, $pos+$needleLength);
            $pos = mb_strpos($haystack, $needle, $pos+$replacementLength);
        }
        return $haystack;
    }
}

$str = <<<EOD
Part A is here:Part B\: is over here:Part C\ is here:Part \\\:D is here
EOD;
    /* Why did I have to use "\\\:D" in a heredoc? No idea.
       I thought "\\:D" would be sufficient, but for some
       reason the backslash was parsed anyways. Which kinda
       makes heredocs useless to me. */

echo "$str<br>", PHP_EOL;

$arr = mb_split('(?<!\\\):', $str);
    // Courtesy of Jerry: http://stackoverflow.com/users/1578604/jerry
foreach ($arr as &$val) {
    $val = mb_str_replace('\:', ':', $val);
}
unset($val);

echo '<pre>', print_r($arr, true), '</pre>';
?>

输出:

Part A is here:Part B\: is over here:Part C\ is here:Part \\:D is here
Array
(
    [0] => Part A is here
    [1] => Part B: is over here
    [2] => Part C\ is here
    [3] => Part \:D is here
)