I've found lot of variations on this subject on both SO and web, but most (if not all) ask for at least one letter and one digit. I need to have at least one letter. I've tried but I haven't make it right, what I need is that String contain only letters, letters + numbers (any order), dashes and spaces are allowed but not at the beginning or the end of the string. Here is how it looks like right now:
protected static final String PATTERN = "[\u00C0-\u017Fa-zA-Z0-9']+([- ][\u00C0-\u017Fa-zA-Z0-9']+)*";
public static void main(String[] args) {
String name;
//name = "Street"; // allowed
//name = "Some-Street"; // allowed
//name = "Street "; // not allowed
//name = " Street"; // not allowed
//name = "Street-"; // not allowed
//name = "-Street"; // not allowed
//name = "Street"; // allowed
//name = "1 st street"; // allowed
//name = "street 5"; // allowed
name = "111"; // NOT allowed
if (!Pattern.matches(PATTERN, name)) {
System.out.println("ERROR!");
} else System.out.println("OK!");
}
}
How do I add check if there is at least one character?
No matter if it is at the beginning or end, or if there is space or dash between it and numbers. There just have to be at least one character.