[Solved] How to create a regexpression for a name with just letters and one “-” i java? [closed]


Not that this is the best idea to verify names. But assuming you’ve settled your requirements, then you can use next example:

if(input.matches("[a-zA-Z]+(\\-[a-zA-Z]+)?")) {
    //OK
} else {
    //Invalid
}

Several examples I’ve tested using this page:

String       matches?
qwe            Yes          
qwe-           No   
qwe-qwe        Yes
qwe-qwe-       No   
qw2e-qwe2      No   
qwe-qwe-qwe    No   

solved How to create a regexpression for a name with just letters and one “-” i java? [closed]