Requires a RegEx that matches only one instance of a pipe symbol

I'm trying to write a RegEx just to match instances of the 'pipe' (|) character NOT followed by one or more additional pipes. Thus, the pipe follows anything other than the pipe.

I have this, but it doesn't seem to work:

/|(?!/|)

      

+3


source to share


3 answers


You avoid yours pipe

wrongly. You need to use backslash

, not slash

. Also, you also need to use negative look-behind

so that the pipe is last

not matched, which probably shouldn't precede the pipe, but shouldn't follow it: -

(?<!\|)\|(?!\|)

      

Generally, I prefer to use a character class rather than escaping when I want to match a metacharacter regex class: -



(?<![|])[|](?![|])

      

But, that's my taste.

+5


source


Ok I found the answer after iterating many times. This will match ONLY one pipe character that has not been continued AND does not follow a pipe.



(?=(\|(?!\|)))((?<!\|)\|)

      

0


source


Match a (beginning of line or non-pipe) to pipe (end of line or non-pipe).

(^|[^|])[|]([^|]|$)

      

0


source







All Articles