Regex: how to know that string contains at least 2 upper case letters?

后端 未结 5 1246
栀梦
栀梦 2020-12-31 23:33

How to know that string contains at least 2 upper case letters? For example these are valid strings \"Lazy Cat\", \"NOt very lazy cat\". Working with Java 1.7.

相关标签:
5条回答
  • 2021-01-01 00:11

    This regex works.

    string.matches(".*[A-Z].*[A-Z].*")
    
    0 讨论(0)
  • 2021-01-01 00:12

    Try this:

    string.matches("[A-Z]+.*[A-Z]+");
    
    0 讨论(0)
  • 2021-01-01 00:16

    You have somany regex answers right now,

    Go for it if you don't want to use **regex**,

    String someString = "abcDS";
    int upperCount = 0;
    for (char c : someString.toCharArray()) {
        if (Character.isUpperCase(c)) {
            upperFound++;
        }
    }
    // upperFound  here weather >2 or not
    
    0 讨论(0)
  • 2021-01-01 00:20

    I'll now show you a full solution, I'll guide you.

    If you don't want to use regex, you can simply loop on the String, chat by char and check whether it's an upper case:

    for (int i=0;i<myStr.length();i++)
    {
         //as @sanbhat suggested, use Character#isUpperCase on each character..
    }
    
    0 讨论(0)
  • 2021-01-01 00:23

    Try with following regex:

    "^(.*?[A-Z]){2,}.*$"
    

    or

    "^(.*?[A-Z]){2,}"
    
    0 讨论(0)
提交回复
热议问题