Java - regex for full name

How to check the correct expression for a full name? I need only alphabets (not numeric) and only spaces for regular expression. This is what I have done so far. Could you help me fix the regex? Many thanks

public static boolean isFullname(String str) { boolean isValid = false; String expression = "^[a-zA-Z][ ]*$"; //I know this one is wrong for sure >,< CharSequence inputStr = str; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputStr); if (matcher.matches()) { isValid = true; } return isValid; } 
+6
source share
6 answers

This problem of name identification is very culturally oriented, and in fact, it does not hope to work reliably. I would recommend against this, since there is an NO canonical form for what constitutes the name of a person in any country, anywhere on Earth that I know of. I could legally change my name to #&*∫Ω∆ Smith , and that would not fit into any algorithm. This is not that this particular example is what many people do, but many people don’t think outside the ASCII table when considering data entry and that this will lead to problems.

You may object to the likelihood of this event, but in a global world it is increasingly unlikely that ALL of your users will have English language scripts for their names. It is also very possible that you will have users from cultures that do not have the concept of first name / last name. Do not assume that even if your application works only in this country, some of your users will not be from other places (people constantly move from country to country, and some of them may want to use your software).

Protect your application against SQL injection for fields like this (if you store them in the database) and leave that to that.

+20
source

This method checks the name and returns false if the name has nothing or has numbers or special characters:

 public static boolean isFullname(String str) { String expression = "^[a-zA-Z\\s]+"; return str.matches(expression); } 
+5
source

What was asked for

Instead of "^[a-zA-Z][ ]*$" you want "^[a-zA-Z ]*$" . There are several answers that refer to \s , but you do not want them because they give you another white space, such as tabs.

Additional General Examples

On the side of the note, there are first names and last names containing hypens, for example Mary-Ann for a first or hyphen last name, for example Jones-Garcia . There are also names that have periods, such as St. Marc St. Marc . Finally, you have ' in some names, such as O'Donnel .

Side note

From a legal point of view, you can change your name Little Bobby Drop tables ... or include other random characters ... but I'm not sure how many systems are really suitable for such things.

If you need a general case (around the world), then do not limit the fields to any type of character, since you can have names in Greek letters, Cyrillic letters, Chinese letters, etc. There are also asymmetric English characters and other things like German umlaut.

If you are worried about SQL injection, use parameterized queries instead of dynamic queries.

Recommended Solution

If you are only worried about English letters and want to use a regular expression that processes the above examples, you can use "^[a-zA-Z \-\.\']*$"

+1
source

[AZ] matches one uppercase letter (first letter of the name)

[az]* matches any number of small letters (other letters or name)

(\s) matches 1 space character (space between names)

+ one or more previous expressions (for matching multiple names)

together:

- matches first names / lastname -
^([AZ][az]*((\s)))+[AZ][az]*$

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

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

0
source
 public static boolean isFullname(String str) { return str.matches("^[a-zA-z ]*$"); } 

this method returns true if the string contains only alphabets and spaces.

But it also returns true if the string does not contain anything.

-2
source

Use \s for spaces. You can also use String.matches(regex) to check if a string matches a regular expression.

 public static boolean isFullname(String str) { String expression = "^[a-zA-Z\\s]*$"; return str.matches(expression); } 
-2
source

Source: https://habr.com/ru/post/896990/


All Articles