I'm trying to write a RegEx to only match instances of the 'pipe' character (|) that are NOT followed by one or more further pipes. So a pipe followed by anything other than a pipe.
I have this but it doesn't appear to be working:
/|(?!/|) I'm trying to write a RegEx to only match instances of the 'pipe' character (|) that are NOT followed by one or more further pipes. So a pipe followed by anything other than a pipe.
I have this but it doesn't appear to be working:
/|(?!/|) You are escaping your pipe wrongly. You need to use backslash, and not slash. Apart from that, you also need to use negative look-behind, so that the last pipe is not matched, which is probably not preceded by a pipe, but not followed by it: -
(?<!\|)\|(?!\|) Generally, I prefer to use character class rather than escaping if I want to match regex meta-character class: -
(?<![|])[|](?![|]) But, it's my taste.
Okay, I found the answer, after much iteration. This will match ONLY a single pipe character not proceeded AND not followed by a pipe.
(?=(\|(?!\|)))((?<!\|)\|)