Your input sample is "lastname, firstname middlename" - with this you can use the following regexp to extract lastname, firstname and middlename (with the addition that there can be several spaces, and that there can be both capital and non-capital letters in the strings - also all parts are required):
String input = "Lastname, firstname middlename"; String regexp = "([A-Za-z]+),\\s+([A-Za-z]+)\\s+([A-Za-z]+)"; Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(input); matcher.find(); System.out.println("Lastname : " + matcher.group(1)); System.out.println("Firstname : " + matcher.group(2)); System.out.println("Middlename: " + matcher.group(3));
Short description:
([A-Za-z]+) First capture group - matches one or more letters to extract the last name ,\\s+ Capture group is followed by a comma and one or more spaces ([A-Za-z]+) Second capture group - matches one or more letters to extract the first name \\s+ Capture group is followed by one or more spaces ([A-Za-z]+) Third capture group - matches one or more letters to extract the middle name
This only works if your names contain only latin letters - perhaps you should use a more open correspondence for characters:
String input = "Müller, firstname middlename"; String regexp = "(.+),\\s+(.+)\\s+(.+)";
This matches any character for the name, name and middlame name.
If spaces are optional (only the first occurrence may be optional, otherwise we cannot distinguish between the name and the name middlame), then use * instead of + :
String input = "Müller,firstname middlename"; String regexp = "(.+),\\s*(.+)\\s+(.+)";
As @Elliott mentions, there may be other possibilities, such as using String.split() or String.indexOf() with String.substring() - regular expressions are often more flexible, but harder to maintain, especially for complex expressions.
In any case, implement unit tests with the maximum possible inputs (including invalid ones) so that you can verify that your algorithm remains valid after changing it.