donjus
donjus

Reputation: 103

Content in between parenthesis regex

I am quite new to regular expressions and may need some help.

I would like to get all expressions that are in between of parenthesis and end with a comma followed by a few digits.

for example asdasd alfalfa (asasdasd, 2002) asdasd fasted (asdasd) sfasadas (asdd,2333)

I already got the last part of this problem working by writing ,\s?\d+\) but I am struggling to achieve a solution for the first part of my problem.

Anyone got an idea how to get this working?

Upvotes: 1

Views: 34

Answers (1)

Bohemian
Bohemian

Reputation: 424993

Try this:

\([^,)]+, ?\d+\)

See live demo.

An important trick here is the closing bracket ) in the character class [^,)]+, which prevents the match spanning across multiple pairs of brackets, ie matching from the opening bracket of an earlier bracketed term to the closing bracket of a later bracketed term.

If you only want the term before the comma (unclear because your example and your question text don't align on this point), convert the latter part of the regex to a look ahead:

(?<=\()[^,)]+(?=, ?\d+\))

See live demo.

Upvotes: 1

Related Questions