'Regular expression to match optional end of string

Given the following:

"John Smith"
"John Smith (123)"
"John Smith (123) (456)"

I'd like to capture:

"John Smith"
"John Smith", "123"
"John Smith (123)", "456"

What Java regex would allow me to do that?

I've tried (.+)\s\((\d+)\)$ and it works fine for "John Smith (123)" and "John Smith (123) (456)" but not for "John Smith". How can I change the regex to work for the first input as well?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source