Java通过正则表达式提取数字串或者字符串

1。通过rex:("\\d+")来提取字符串

Scanner sc = new Scanner(System.in);
String[] a = sc.nextLine().split("\\d+");
for(String s : a){
     System.out.print(s+" ");
}

2。通过rex: [^0-9] 来提取数字

        Scanner sc = new Scanner(System.in);
        //String[] a = sc.nextLine().split("\\d+");
        Pattern pattern = Pattern.compile("[^0-9]");
        String a = sc.nextLine();
        Matcher a1 = pattern.matcher(a);
        //System.out.println(a1.replaceAll(" "));
        String[] dis = a1.replaceAll(" ").split("\\s+");  //到这儿为为止已经提取了数子串
//====下面输出数字串中最大的一个
        int temp = 0;
        for(int i = 0; i < dis.length-1; i++){
            if(dis[i].length() < dis[i+1].length()){
                temp = i+1;
            }
        }
        System.out.println(dis[temp]);



猜你喜欢

转载自blog.csdn.net/yguoelect/article/details/73388977
今日推荐