How do I get the characters inside a pair of parentheses which are immediately followed by a specific string?

My string looks like this:

(left.e0_coefficient*right.Scalar) e₀ + (left.e1_coefficient*right.Scalar) e₁ + (left.e2_coefficient*right.Scalar) e₂ + (left.e3_coefficient*right.Scalar) e₃

I want to get the expression thats contained in the parentheses preceeding e₂ => left.e2_coefficient*right.Scalar

I tried using \((.+?)\) e₂ however despite it being lazy (non-greedy) the capturing group still contains everything from the first parenthesis in the string to the one immediately before e₂ => left.e0_coefficient*right.Scalar) e₀ + (left.e1_coefficient*right.Scalar) e₁ + (left.e2_coefficient*right.Scalar

I think I might have to use a positive-lookbehind or some other form of non-greediness, but I cant figure it out.

>Solution :

Regex for your case: \(([^()]+?)\) e₂

About laziness: Laziness works not like this. Laziness modifier changes behavior of group for which it’s applied, but in your case opening parenthesis is already matched previously.

Edit: Word of caution: this regex applies only if you don’t have nested parenthesis. If nested parenthesis possible there are two options:

  1. You parse whole expression with something like \([^()]+?\) e0 + \([^()]+?\) e1 + \(([^()]+?)\) e₂ + \([^()]+?\) e3 (if possible)
  2. You try to parse expression respecting the balance of parentheses. It is generally not easy or obvious task. (sometimes, depending on regex engine, could be impossible).

Leave a Reply