Regular Expression for UpperCase Letters In A String

前端 未结 8 1551
野性不改
野性不改 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);
       }
    }
    

提交回复
热议问题