regular expression to match pipe separated strings with pipe escaping

血红的双手。 提交于 2019-12-01 13:25:27
Split  by this (?<!\\)\|

See demo.The lookbehind makes sure | is not after \.

https://regex101.com/r/pM9yO9/15

This should work too:

((?:[^\\|]+|\\\|?)+)

The regex will capture everything up to a single | (including \|)

DEMO

An other way with php, using strtr that replace \| with a placeholder:

$str = 'field a|field b|field\|with\|pipe\|inside';
$str = strtr($str, array('\|' => '#'));
$result = array_map(function ($i) {
    return strtr($i, '#', '|');
}, explode('|', $str));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!