Regular Expression for UpperCase Letters In A String

前端 未结 8 1553
野性不改
野性不改 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:54

    This should do what you're after,

    @Test
    public void testCountTheNumberOfUpperCaseCharacters() {
      String testStr = "abcdefghijkTYYtyyQ";
      String regEx = "[A-Z]+";
      Pattern pattern = Pattern.compile(regEx);
      Matcher matcher = pattern.matcher(testStr);
      int count = 0;
      while (matcher.find()) {
        count+=matcher.group(0).length();
      }
      System.out.printf("Found %d, of capital letters in %s%n", count, testStr);
    }
    
    0 讨论(0)
  • 2021-02-20 03:54

    You can also use Java Regex, for example:

    .+[\p{javaUpperCase}].+ 
    

    An example from my work project:

    0 讨论(0)
提交回复
热议问题