1

I want to match '|| C \n' in the following string:

|| A || B || C \n

With a regexp that is something like '\|\|not(\|\|).*?\n', so that all '||' are excluded except the last one.

How does one do this?

Peter
  • 367
  • 1
  • 4
  • 12
  • 2
    You are asking for [`\|\|(?:(?!\|\|).)*?\n`](https://regex101.com/r/4utNDQ/1). But probably, you may also use `\|\|(?:(?!\|\|).)*$` or `\|\|(?!.*\|\|).*`. – Wiktor Stribiżew Jul 20 '18 at 21:32
  • @Peter - `all '||' are excluded except the last one` Did I answer your question ? Or, was the last 3 bars ok with you per strubnutz https://regex101.com/r/4utNDQ/2 ? –  Jul 22 '18 at 21:11

1 Answers1

0

You can't just look for 2 bars, then make sure no other double bars exist.
Have to protect against a triple bar ||| first.

\|\|(?!\|)[^|]*(?:\|(?!\|)[^|]*)*$

Readable version

 \|\|                          # Double bar
 (?! \| )                      # Cannot be a third bar
 [^|]*                         # Optional non-bars
 (?:                           # Cluster
      \|                            # Bar
      (?! \| )                      # if not followed by bar
      [^|]*                         # Optional non-bars
 )*                            # End, optionally 0 to many times
 $                             # EOS