I'd like to get the single strings of a pipe-separated string, with "pipe escaping" support, e.g.:
fielda|field b |field\|with\|pipe\|inside
would get me:
array("fielda", "field b ", "field|with|pipe|inside")
How would I reach that goal with a regular expression?
Split by this (?<!\\)\|
See demo.The lookbehind makes sure | is not after \.
This should work too:
((?:[^\\|]+|\\\|?)+)
The regex will capture everything up to a single | (including \|)
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));
来源:https://stackoverflow.com/questions/28200875/regular-expression-to-match-pipe-separated-strings-with-pipe-escaping