Java - Regex for Full Name

后端 未结 6 1781
谎友^
谎友^ 2020-12-28 21:40

How can I validate regex for full name? I only want alphabets (no numericals) and only spaces for the regex. This is what I have done so far. Would you please help me fix th

6条回答
  •  一向
    一向 (楼主)
    2020-12-28 21:57

    [A-Z] match a single capital letter (first letter of name)

    [a-z]* match any number of small letters (other letters or name)

    (\s) match 1 whitespace character (the space between names)

    + one or more of the previous expression (to match more than one name)

    all together:

    - matches first names / lastname -
    ^([A-Z][a-z]*((\s)))+[A-Z][a-z]*$

    or to match names like DiMaggio St. Croix, O'Reilly and Le-Pew. You can add similar characters like the 'ᶜ' in MᶜKinley as you remember them or come across people with those less common characters in their name

    ^([A-z\'\.-ᶜ]*(\s))+[A-z\'\.-ᶜ]*$

提交回复
热议问题