Java Regex to Validate Full Name allow only Spaces and Letters

前端 未结 14 2352
抹茶落季
抹茶落季 2020-11-28 04:50

I want regex to validate for only letters and spaces. Basically this is to validate full name. Ex: Mr Steve Collins or Steve Collins I tried this regex.

相关标签:
14条回答
  • 2020-11-28 05:18

    You could even try this expression ^[a-zA-Z\\s]*$ for checking a string with only letters and spaces (nothing else).

    For me it worked. Hope it works for you as well.

    Or go through this piece of code once:

        CharSequence inputStr = expression;
        Pattern pattern = Pattern.compile(new String ("^[a-zA-Z\\s]*$"));
        Matcher matcher = pattern.matcher(inputStr);
        if(matcher.matches())
        {
             //if pattern matches
        }
        else
        {
             //if pattern does not matches
        }
    
    0 讨论(0)
  • 2020-11-28 05:19

    What about:

    • Peter Müller
    • François Hollande
    • Patrick O'Brian
    • Silvana Koch-Mehrin

    Validating names is a difficult issue, because valid names are not only consisting of the letters A-Z.

    At least you should use the Unicode property for letters and add more special characters. A first approach could be e.g.:

    String regx = "^[\\p{L} .'-]+$";
    

    \\p{L} is a Unicode Character Property that matches any kind of letter from any language

    0 讨论(0)
提交回复
热议问题