比如 http://www.123.com:8080/xxxx/xxxx/xxx2.html我如何只提取最后的那个字符串的数字,而不管其他的,就是xxx2.html中的2啊

解决方案 »

  1.   


    String str="http://www.123.com:8080/xxxx/xxxx/xxx2.html";
    Matcher m=Pattern.compile("(\\d+)\\.[^/]*$").matcher(str);
    if(m.find()){
    System.out.println(m.group(1));
    }
      

  2.   


    /*
    比如 http://www.123.com:8080/xxxx/xxxx/xxx2.html我如何只提取最后的那个字符串的数字,而不管其他的,就是xxx2.html中的2啊*/import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test1{
    public static void main(String[] args){
    String content = "http://www.123.com:8080/xxxx/xxxx/xxx2.html";
    String regex = "(\\d+)\\.html$";
    Matcher matcher = Pattern.compile(regex).matcher(content);
    if(matcher.find()){
    System.out.println(matcher.group(1));
    }
    }
    }
      

  3.   

    public static void main(String[] args) {
    String s = "http://www.123.com:8080/xxxx/xxxx/xxx2.html";
    // 以"/"隔开放入数组中
    String arr[] = s.split("\\/");
    // 取数组中最后一个元素,并把不是数字部分变为空
    s = arr[arr.length - 1].replaceAll("\\D", "");
    System.out.println(s);
    }
    输出:
    2