[a-zA-Z. ]+ allows you to use letters, periods and spaces.
import java.util.regex.*;
public class Test {
public static void main(String [] args) throws Exception {
String RE = "[a-zA-Z. ]+";
String name = args[0];
Pattern pattern = Pattern.compile(RE);
Matcher m = pattern.matcher(name);
System.err.println("`" + RE +
(m.matches()?"' matches `":"' does not match `") +
name + "'");
}
}
Duration:
$ java Test "R. Robert"
`[a-zA-Z., ]+' matches `R. Robert'
$ java Test "R.-Robert"
`[a-zA-Z., ]+' does not match `R.-Robert'
source
share