-3

Please help create a regular expression that would be allocated "|" character everywhere except parentheses.

example|example (example(example))|example|example|example(example|example|example(example|example))|example

After making the selection should have 5 characters "|" are out of the equation. I want to note that the contents within the brackets should remain unchanged including the "|" character within them.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • 1
    Possible duplicate of [Regex - how to match everything except a particular pattern](http://stackoverflow.com/questions/611883/regex-how-to-match-everything-except-a-particular-pattern) – Alvaro Silvino Oct 04 '15 at 20:12
  • I read that article that you sent, but I regret not describe the solution of this problem – Jay Palmer Oct 04 '15 at 20:55

1 Answers1

0

Considering you want to match pipes that are outside any set of parentheses, with nested sets, here's the pattern to achieve what you want:

Regex:

(?x)                     # Allow comments in regex (ignore whitespace)
(?:                      # Repeat *
    [^(|)]*+             #  Match every char except ( ) or |
    (                    #  1. Group 1
        \(               #    Opening paren
        (?:              #    chars inside:
            [^()]++      #     a. everything inside parens except nested parens
          |              #      or
            (?1)         #     b. nested parens (recurse group 1)
        )                #
        \)               #   Until closing paren.
    )?+                  #   (end of group 1)
)*+                      #
\K                       #  Keep text out of match
\|                       #  Match a pipe

regex101 Demo

One-liner:

(?:[^(|)]*+(\((?:[^()]++|(?1))\))?+)*+\K\|

regex101 Demo


This pattern uses some advanced features:

Mariano
  • 6,423
  • 4
  • 31
  • 47