tbop
tbop

Reputation: 404

How to match un-nested groups between parenthesis only?

I am trying to match chains of characters that are within single opening and closing parenthesis.

In the following examples only the first two lines should return ab.
The other ones shouldn't match anything

ab(ab)ac => ab
(ab)ndn => ab
ab(ab(ac)an) => void
ab((ab)ab)ab => void
ab(ab(abb))ab => void
ab(ab(ab(ab))ab) => void

This is as far as I could go atm I do not know why the third line is still matched. https://regex101.com/r/weGhVz/2

Upvotes: 0

Views: 47

Answers (1)

Ashkan
Ashkan

Reputation: 1085

You could use regex negative lookahead feature to make sure there is no parentesis after the first closing.

\((\w*)\)(?!\w*\))

This produces the output you want. Group one gives the sequence inside the parenthesis

Upvotes: 1

Related Questions