Regular Expression for UpperCase Letters In A String

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

    In this example i'm using a regex(regular Expression) to count the number of UpperCase and LowerCase letters in the given string using Java.

    import java.util.regex.*;
    import java.util.Scanner;
    import java.io.*;
    public class CandidateCode {
        public static void main(String args[] ) throws Exception {
            Scanner sc= new Scanner(System.in);
        //  Reads the String of data entered in a line
            String str = sc.nextLine();
    
        //counts uppercase letteres in the given String 
            int countuc = str.split("([A-Z]+?)").length; 
    
        //counts lowercase letteres in the given String 
            int countlc = str.split("([a-z]+?)").length; 
    
            System.out.println("UpperCase count: "+countuc-1);
            System.out.println("LowerCase count: "+countlc-1);
       }
    }
    
    0 讨论(0)
  • 2021-02-20 03:43

    Your pattern as you've written it looks for 1 or more capital letters between the beginning and the end of the line...if there are any lowercase characters in the line it won't match.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-20 03:47

    It doesn't work because you have 2 problems:

    1. Regex is incorrect, it should be "[A-Z]" for ASCII letter or \p{Lu} for Unicode uppercase letters
    2. You're not calling while (matcher.find()) before matcher.groupCount()

    Correct code:

    public void testCountTheNumberOfUpperCaseCharacters() {
        String testStr = "abcdefghijkTYYtyyQ";
        String regEx = "(\\p{Lu})";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(testStr);
        while (matcher.find())
            System.out.printf("Found %d, of capital letters in %s%n", 
              matcher.groupCount(), testStr);
    
    }
    

    UPDATE: Use this much simpler one-liner code to count number of Unicode upper case letters in a string:

    int countuc = testStr.split("(?=\\p{Lu})").length - 1;
    
    0 讨论(0)
  • 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);
    }
    

    }

    0 讨论(0)
  • 2021-02-20 03:52

    It should find upper case letters in the given string and give me the count.

    No, it shouldn't: the ^ and $ anchors prevent it from doing so, forcing to look for a non-empty string composed entirely of uppercase characters.

    Moreover, you cannot expect a group count in an expression that does not define groups to be anything other than zero (no matches) or one (a single match).

    If you insist on using a regex, use a simple [A-Z] expression with no anchors, and call matcher.find() in a loop. A better approach, however, would be calling Character.isUpperCase on the characters of your string, and counting the hits:

    int count = 0;
    for (char c : str.toCharArray()) {
        if (Character.isUpperCase(c)) {
            count++;
        }
    }
    
    0 讨论(0)
提交回复
热议问题