Reputation: 66
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:
/|(?!/|)
Upvotes: 3
Views: 1619
Reputation: 66
Okay, I found the answer, after much iteration. This will match ONLY a single pipe character not proceeded AND not followed by a pipe.
(?=(\|(?!\|)))((?<!\|)\|)
Upvotes: 1
Reputation: 1223
Match a (start of line or a non-pipe) a pipe (an end of line or a non-pipe).
(^|[^|])[|]([^|]|$)
Upvotes: 0
Reputation: 213223
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.
Upvotes: 5