Java Regex to Validate Full Name allow only Spaces and Letters

前端 未结 14 2529
抹茶落季
抹茶落季 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:00

    My personal choice is: ^\p{L}+[\p{L}\p{Pd}\p{Zs}']*\p{L}+$|^\p{L}+$, Where:

    ^\p{L}+ - It should start with 1 or more letters.

    [\p{Pd}\p{Zs}'\p{L}]* - It can have letters, space character (including invisible), dash or hyphen characters and ' in any order 0 or more times.

    \p{L}+$ - It should finish with 1 or more letters.

    |^\p{L}+$ - Or it just should contain 1 or more letters (It is done to support single letter names).

    Support for dots (full stops) was dropped, as in British English it can be dropped in Mr or Mrs, for example.

提交回复
热议问题