Regular Expression for UpperCase Letters In A String

前端 未结 8 1573
野性不改
野性不改 2021-02-20 03:15

For the life of me, I can\'t figure out why this regular expression is not working. It should find upper case letters in the given string and give me the count. Any ideas are we

8条回答
  •  一个人的身影
    2021-02-20 03:48

    Change the regular expression to [A-Z] which checks all occurrences of capital letters

    Please refer the below example which counts number of capital letters in a string using pattern

    @Test
    public void testCountTheNumberOfUpperCaseCharacters() {
        Pattern ptrn = Pattern.compile("[A-Z]");
        Matcher matcher = ptrn.matcher("ivekKVVV");
        int from = 0;
        int count = 0;
        while(matcher.find(from)) {
            count++;
            from = matcher.start() + 1;
        }
        System.out.println(count);
    }
    

    }

提交回复
热议问题