Regular Expression for UpperCase Letters In A String

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

    1. You didn't call matches or find on the matcher. It hasn't done any work.

    2. getGroupCount is the wrong method to call. Your regex has no capture groups, and even if it did, it wouldn't give you the character count.

    You should be using find, but with a different regex, one without anchors. I would also advise using the proper Unicode character class: "\\p{Lu}+". Use this in a while (m.find()) loop, and accumulate the total number of characters obtained from m.group(0).length() at each step.

提交回复
热议问题