/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String[] lst = new String[] {"John Smith","John Smith (123)","John Smith (123) (456)"};
		Pattern p = Pattern.compile("(.+?)(?:\\s\\((\\d+)\\))?");
		for (String s: lst) {
			System.out.println("--- Next string ---");
			Matcher m = p.matcher(s);
    		if (m.matches()) {
      			System.out.println(m.group(1));
      			if (m.group(2) != null)
      				System.out.println(m.group(2));
    		}
		}
	}
}