首页 > 如何取出字符串中特定数字

如何取出字符串中特定数字

取出类似http://examle.com/xxx/yyy368/pichdjejjdhe9.jpg
http://examle.com/xxx/yyy245/pichdjejjdhe9.png
最后的数字9,数字固定在文件名最后


使用正则表达式


var url  ="http://examle.com/xxx/yyy368/pichdjejjdhe9.jpg";
console.log(url.match(/\d(?=\.)/)[0]);

能取你那个9


var str = "http://examle.com/xxx/yyy368/pichdjejjdhe9.jpg";
var reg = /http:\/\/\w+\.com\/\w+\/\w+\/[a-zA-Z]+(\d)\.\w+/;
reg.exec(str)[1];

    String imgUrl = "http://examle.com/xxx/yyy245/pichdjejjdhe9.png";
    String fileName = imgUrl.substring(imgUrl.lastIndexOf("/") + 1,imgUrl.lastIndexOf("."));

    String pattern = "\\d+";
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(fileName);
    
    int start = 0;
    int end = 0;
    
    while(m.find()){
        start = m.start();
        end = m.end();
    }
    
    int num = Integer.parseInt(fileName.substring(start, end));
    System.out.println(num);
【热门文章】
【热门文章】