Updated regex and logic to handle parentheses.