RegExp: Matching Balanced Parenthesis and Quotes (greedy, non-recursive)

Solution

I need to match all the text within balanced parens, single quotes, or double quotes, but don’t snag on the content within, i.e. be greedy! “Balanced” means there should be a corresponding single or double quote, but not mix them, or there should be a closing paren for each opening paren.

PCRE:

The match will be in capturing group #3.

In short, this regular expression uses a positive lookahead (?=...)  within the lookaround conditional  (?...)  and the backreference \2  to match the initiating character. If it is a single quote or double quote, it was contained within \2 , but if it was an open paren ( , it is not contained in a capture group and therefore it should match a close paren. In retrospect, this could be enhanced, and also needs some tweaks for JavaScript compatibility since lookaround conditionals are not supported.

Do note that this matches the beginning and ending of the string ^$ , so may require tweaking for other applications.

Test Cases

regex101 link: https://regex101.com/r/fG4rZ8/3

Matches:

These will match hello , hello world , or in some cases 'hello world  for the last test case.

Doesn’t Match: