0

I searched in net for all regex exp but dint find any matching my needs.

regular expression to allow spaces between words

Regular expression to allow alphanumeric, max one space etc

Regex, every non-alphanumeric character except white space or colon

In non of the above I got a solution :

I need to check the Nam is valid if
=> First letter every word should be caps
=> Rest all letters of each word should be small
=> Name should have only alpha char A-Z and a-z
=> First word Length should be min 3
=> Name shud not have more than one space between words

Ex :
sujay => false
Sujay => true

Sujay u => false
Sujay U => true

Sujay U n => false
Sujay U N => true

SuJay U => false
Sujay UN => false
Sujay Uls => true

Sujay9 => false
Su => false
Su U => false
Sujay U N => true
Sujay Uls Nat=> true

|*| Check function used :

static boolean chkNamVldFnc(String namVar)
{
    String namRegExpVar = "[A-Z][A-Za-z ]{2,}";

    Pattern pVar = Pattern.compile(namRegExpVar);
    Matcher mVar = pVar.matcher(namVar);
    return mVar.matches();
}

|*| Try 1 :

String namRegExpVar = "[A-Z][A-Za-z ]{2,}";

|*| Try 2 :

String namRegExpVar = "[A-Z][a-z]{2,}+//s[A-Z][a-z]{2,}";

|*| Try 3 :

String NamRegExpVar = "[A-Z][a-z]{2,}||[A-Z][a-z]{2,}+//s[A-Z][a-z]";

Kindly help me with Proper Regular Exp :

I also want to know why we shud start Reg Exp with ^ and end with $

Sujay U N
  • 4,534
  • 8
  • 46
  • 79

1 Answers1

2

Try:

^[A-Z][a-z]{2,}(?: [A-Z][a-z]*)*$
  • First name must start with letter A-Z, followed by at least 2 letters a-z
  • Optionally, there can be names following the first name, seperated by a space and beginning with letter A-Z, and followed by optional letters a-z
yas
  • 3,428
  • 4
  • 22
  • 35