3

I want to use regex to validate names. The names must contain, first name, middle name, last name (not necessarily all). But I also want to impose a condition that the name must be of at least four characters. I have found regex to validate full name here Java Regex to Validate Full Name ... and found regex to check for checking of at least three chars (alphabets) in a string here Regex to check for at least 3 characters. But I am not sure how to combine these two to obtain the desired result. Please help me to achieve the desired Regex, so that I can complete my project.

Community
  • 1
  • 1
SKG
  • 410
  • 1
  • 4
  • 17
  • What should be the final result? What have you tried? Where did you fail? The `"^[\\p{L} .'-]+$"` regex allows letters, space, dot, apostrophe and a hyphen, one or more times. The limited quantifier allowing three or more is `{3,}`. Now, what are valid and invalid cases? – Wiktor Stribiżew Feb 14 '16 at 15:00
  • The valid cases must be john c mather or steve jobs but the invalid cases will be like dj, p o lice, mc dew carp etc, I mean to say the first name must have atleast four chararcters – SKG Feb 17 '16 at 12:10
  • Try [`String pattern = "^\\pL{4,}(?:\\s+\\pL+)?(?:\\s+\\pL+)?$"`](https://regex101.com/r/hY5bM1/1). With `.matches()`, you can remove `^` and `$`. See [this demo](http://ideone.com/mGSfLf). – Wiktor Stribiżew Feb 17 '16 at 12:16
  • Actually, I am using the regex in google form's input validation to validate the names of people submitted, but this regex is also not working there, please help me – SKG Feb 17 '16 at 12:52
  • Then why tag `java`? Use `^[a-zA-Z]{4,}(?: [a-zA-Z]+)?(?: [a-zA-Z]+)?$` – Wiktor Stribiżew Feb 17 '16 at 12:56
  • I am very sorry for that, I will remove it, I mistakenly did this, Thank you very much for helping ! – SKG Feb 17 '16 at 13:04
  • Can you please downvote the below answer by SilentStorm so that it can be removed, I am unable to do that because I mistakenly upvoted it. – SKG Feb 17 '16 at 13:06
  • Does it mean my above solution works? I can post it as an answer if it did. BTW, no need for me to downvote SilentSorm's answer. I can edit it, and you will remove the upvote. – Wiktor Stribiżew Feb 17 '16 at 13:11
  • Yes your answer worked successfully, and I would be happy with what do you suggest – SKG Feb 17 '16 at 13:19
  • I suggest to have a look at: http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – Toto Feb 17 '16 at 14:05
  • Thanks, it's very useful ! – SKG Feb 17 '16 at 14:22

2 Answers2

11

You can use

^[a-zA-Z]{4,}(?: [a-zA-Z]+){0,2}$

See the regex demo

This will work with names starting with both lower- and upper-cased letters.

  • ^ - start of string
  • [a-zA-Z]{4,} - 4 or more ASCII letters
  • (?: [a-zA-Z]+){0,2} - 0 to 2 occurrences of a space followed with one or more ASCII letters
  • $ - end of string.

If you need to restrict the words to start with Uppercase letters, you can use

^[A-Z][a-zA-Z]{3,}(?: [A-Z][a-zA-Z]*){0,2}$
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
1

It might be a bit overkill but:

([A-Z][a-z]{3,} )([A-Z][a-z]{3,} )?([A-Z][a-z]{3,})

should do the trick. It matches words that start with a capitalized letter followed by 3 or more lowercase letter -> words have a length of four. The middle-name is optional and the last name doesn't contain a trailing whitespace.


Edit:
If you want to support "fancy" characters (äöü etc.) you can read this question for details. Using the pattern from Java 7 with the UNICODE_CHARACTER_CLASS flag the regex should look like this:

(\\p{Upper}\\p{Lower}{3,} )(\\p{Upper}\\p{Lower}{3,} )?(\\p{Upper}\\p{Lower}{3,})
Community
  • 1
  • 1
SilentStorm
  • 139
  • 1
  • 10
  • 1
    @WiktorStribiżew See [this question](http://stackoverflow.com/a/4307261/4233265) for information on regex for names using "fancy" characters. – SilentStorm Feb 14 '16 at 17:10
  • Why should I? I think it is your answer. – Wiktor Stribiżew Feb 14 '16 at 17:26
  • @WiktorStribiżew I'm sorry but I'm not here to do the work for you, I'm here to help you find an answer to your question. You need to be engaged in solving your problem yourself. – SilentStorm Feb 14 '16 at 17:41
  • I do not have any problems. OP has. If you post an answer it should address OP problem. – Wiktor Stribiżew Feb 14 '16 at 18:00
  • @WiktorStribiżew Yea I'm so sorry I didn't realize you're not OP *self-facepalm* I will edit my answer. – SilentStorm Feb 14 '16 at 18:01
  • Sorry SilentStorm, but your solution is not working, please give correct answers only. for example, it's not working for - Sddd dfndfkn, you can check it yourself – SKG Feb 17 '16 at 12:05
  • You need to modify your regex because for example MdDonald, Williams-Parcker and O'Brian are valid last names – V. Lipunov Mar 14 '20 at 17:36