0

Words with at least 2 Capital letters and with any special letters (like @#$%^&*()_-+= and so on...) optional.

I tried:

 public static boolean isWordHas2Caps(String s) {
        return s.matches("\\b(?:\\p{Ll}*\\p{Lu}){2,}\\p{Ll}*\\b");
    }

But, I am getting

    System.out.println(isWordHas2Caps("eHJHJK"));
    System.out.println(isWordHas2Caps("YUIYUI"));
    System.out.println(isWordHas2Caps("LkfjkdJkdfj"));
    System.out.println(isWordHas2Caps("LLdkjkd"));
    System.out.println(isWordHas2Caps("OhdfjhdsjO"));
    System.out.println(isWordHas2Caps("LLLuoiu9898"));
    System.out.println(isWordHas2Caps("Ohdf&jh/dsjO"));
    System.out.println(isWordHas2Caps("auuuu"));
    System.out.println(isWordHas2Caps("JJJJJJJJ"));
    System.out.println(isWordHas2Caps("YYYY99999"));
    System.out.println(isWordHas2Caps("ooooPPPP"));

Output:

true   eHJHJK
true   YUIYUI
true   LkfjkdJkdfj
true   LLdkjkd
true   OhdfjhdsjO
false   LLLuoiu9898       It should be true but getting false
false   Ohdf&jh/dsjO      It should be true but getting false
false   auuuu
true   JJJJJJJJ
false   YYYY99999        It should be true but getting false
true   ooooPPPP

I think, I should in the regexp and numbers and Special letters. How can I do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sun
  • 3,444
  • 7
  • 53
  • 83

2 Answers2

2

You want to check if there are at least two uppercase letters anywhere in a string that can contain arbitrary chars.

Then, you can use

public static boolean isWordHas2Caps(String s) {
    return Pattern.compile("\\p{Lu}\\P{Lu}*\\p{Lu}").matcher(s).find();
}

See the Java demo.

Alternatively, if you still want to use String#matches you can use the following (keeping in mind that we need to match the entire string):

public static boolean isWordHas2Caps(String s) {
    return s.matches("(?s)(?:\\P{Lu}*\\p{Lu}){2}.*");
}

The (?s)(?:\\P{Lu}*\\p{Lu}){2}.* regex matches

  • (?s) - the Pattern.DOTALL embedded flag option (makes . match any chars)
  • (?:\P{Lu}*\p{Lu}){2} - two occurrences of any zero or more chars other than uppercase letters and then an uppercase letter
  • .* - the rest of the string.

Your code did not return expected results because all of them contain non-letter characters, while String#matches() requires a full string match against a pattern, and yours matches strings that contains letters only.

That is why you should

  • Make sure you can match anywhere inside a string, and Matcher.find does this job best
  • \p{Lu}\P{Lu}*\p{Lu} pattern will find any sequence of an uppercase letter + any zero or more non-letters + an uppercase letter
  • Alternatively, you can use (?s)(?:\P{Lu}*\p{Lu}){2}.* regex to match a full string that contains at least two uppercase letters.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

Update:

A valuable comment from anubhava:

Probably s.matches("(?:\\S*\\p{Lu}){2}\\S*"); may be better

Demo of the above solution.

Original answer:

You can use the regex, \b.*\p{Lu}.*\p{Lu}.*\b as shown below:

public static boolean isWordHas2Caps(String s) {
    return s.matches("\\b.*\\p{Lu}.*\\p{Lu}.*\\b");
}

Demo:

public class Main {
    public static void main(String[] args) {
        System.out.println(isWordHas2Caps("eHJHJK"));
        System.out.println(isWordHas2Caps("YUIYUI"));
        System.out.println(isWordHas2Caps("LkfjkdJkdfj"));
        System.out.println(isWordHas2Caps("LLdkjkd"));
        System.out.println(isWordHas2Caps("OhdfjhdsjO"));
        System.out.println(isWordHas2Caps("LLLuoiu9898"));
        System.out.println(isWordHas2Caps("Ohdf&jh/dsjO"));
        System.out.println(isWordHas2Caps("auuuu"));
        System.out.println(isWordHas2Caps("JJJJJJJJ"));
        System.out.println(isWordHas2Caps("YYYY99999"));
        System.out.println(isWordHas2Caps("ooooPPPP"));
    }

    public static boolean isWordHas2Caps(String s) {
        return s.matches("\\b.*\\p{Lu}.*\\p{Lu}.*\\b");
    }
}

Output:

true
true
true
true
true
true
true
false
true
true
true
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110