Reputation: 3870
I'm actually having some strings that have some data separated by a pipe character, for example:
One | Two | Three
I want to split these strings when I find a pipe, but I also want to be sure that if there is an "escaped pipe" ( \| ) it won't be processed.
So, for example, from this string: Tom | Dick \| and | Harry
I want to obtain an array that contains values: Tom
, Dick \| and
, Harry
For this reason I wrote a small regular expression that searches for a pipe that is not preceded by a backslash: (?<!\\)\|
I have tested this regular expression inside my IDE (PHPStorm, that is Java-based AFAIK) and it was working properly, but when I use it inside a PHP project I'm getting errors; actually I'm testing this code with PHP version 5.3.6
Could you possibly help me and tell me what am I doing wrong, please?
<?php
$cText = "First choice | Second \| choice |Third choice";
// I need to split a string, and to divide it I need to find
// each occurrence of a pipe character "|", but I also have to be sure not
// to find an escaped "|".
//
// What I'm expecting:
// acChoice[0] = "First choice "
// acChoice[1] = " Second \| choice "
// acChoice[2] = "Third choice"
$acChoice = preg_split("/(?<!\\)\|/", $cText);
// Gives this error:
// Warning: preg_split(): Compilation failed: missing ) at offset 8 in - on line 14 bool(false)
$acChoice = mb_split("/(?<!\\)\|/", $cText);
// Gives this error:
// Warning: mb_split(): mbregex compile err: end pattern with unmatched parenthesis in - on line 19 bool(false)
?>
Upvotes: 2
Views: 607
Reputation: 526573
You need to double-escape your backslashes, because they're getting parsed twice: one by the string parser, and then one by the regex engine.
$acChoice = preg_split("/(?<!\\\\)\\|/", $cText);
Upvotes: 3