-1

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.

1 Answers1

1

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).
markalex
  • 8,623
  • 2
  • 7
  • 32
  • 1
    You don't need the question mark to make it non greedy as the negated character class can not cross the `)`. – The fourth bird Mar 12 '23 at 15:33
  • Since it already was there I saved it. Also, for me it is easier to modify such expressions later (less possible hustle with exactly authors problem). – markalex Mar 12 '23 at 15:44