匹配字符串并获取匹配后的内容

时间:2018-12-31 09:11:30

标签: javascript jquery regex

我有一个洪流文件中的内容

d8:announce39:http://torrent.ubuntu.com:6969/announce13:announce-listll39:http://torrent.ubuntu.com:6969/announceel44:http://ipv6.torrent.ubuntu.com:6969/announceee7:comment29:Ubuntu CD releases.ubuntu.com13:creation datei1539860537e4:infod6:lengthi1999503360e4:name30:ubuntu-18.10-desktop-amd64.iso12:piece lengthi524288e6:pieces76280

或者我也可以

d8:announce39:http://torrent.centos.org:6969/announce13:announce-listll39:http://torrent.centos.org:6969/announceel44:http://ipv6.torrent.centos.org:6969/announceee7:comment27:CentOS x86_64 LiveGNOME ISO10:created by13:mktorrent 1.013:creation datei1526053398e4:infod5:filesld6:lengthi1388314624e4:pathl34:CentOS-7-x86_64-LiveGNOME-1804.isoeed6:lengthi454e4:pathl11:sha1sum.txteed6:lengthi1314e4:pathl15:sha1sum.txt.asceed6:lengthi598e4:pathl13:sha256sum.txteed6:lengthi1458e4:pathl17:sha256sum.txt.asceee4:name30:CentOS-7-x86_64-LiveGNOME-180412:piece lengthi524288e6:pieces52980:

我想检索名称部分,例如ubuntu-18.10-desktop-amd64.iso 这是我尝试过的方法,但它会检查所有内容:

$.get('search', function(data) {
    var lines = data.split("\n");
    var $result = false
    var url_check 
    var search= [];
    $.each(lines, function(n, data) {
        search.push(data)
    })
}).done(function(search){
    search= search.split("\n");
    var $result = [];
    $.each(search, function(n, search) {
        var regex = new RegExp('^(?=.*' + search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').split(/\\?[\s,_.:*-]+/).join(')(?=.*') + ')', 'gi');
        if(regex.test(url_check) === true){
            $result.push('true');
        }else{
            $result.push('false');
        }
    })
    console.log($result)
    if($result.indexOf("true") !== -1){
        alert('Found !') 
    }else {
        alert('Not found !');
    }
})

文件内容搜索

*Ubuntu.18*
*centos 7*

1 个答案:

答案 0 :(得分:1)

根据BitTorrent specification,这些结尾数字与以下字符串的长度相匹配。例如

ubuntu-18.10-desktop-amd64.iso

长30个字符,因此它前面的数字是30:

30:ubuntu-18.10-desktop-amd64.iso

因此,纯正则表达式解决方案将无法真正起作用,至少不能以任何简洁的方式起作用。您可以提取name<digits>:和它后面的字符串 ,然后在Javascript中使用slice提取冒号后面的<digits>个字符:< / p>

const input = [
  'd8:announce39:http://torrent.ubuntu.com:6969/announce13:announce-listll39:http://torrent.ubuntu.com:6969/announceel44:http://ipv6.torrent.ubuntu.com:6969/announceee7:comment29:Ubuntu CD releases.ubuntu.com13:creation datei1539860537e4:infod6:lengthi1999503360e4:name30:ubuntu-18.10-desktop-amd64.iso12:piece lengthi524288e6:pieces76280',
  'd8:announce39:http://torrent.centos.org:6969/announce13:announce-listll39:http://torrent.centos.org:6969/announceel44:http://ipv6.torrent.centos.org:6969/announceee7:comment27:CentOS x86_64 LiveGNOME ISO10:created by13:mktorrent 1.013:creation datei1526053398e4:infod5:filesld6:lengthi1388314624e4:pathl34:CentOS-7-x86_64-LiveGNOME-1804.isoeed6:lengthi454e4:pathl11:sha1sum.txteed6:lengthi1314e4:pathl15:sha1sum.txt.asceed6:lengthi598e4:pathl13:sha256sum.txteed6:lengthi1458e4:pathl17:sha256sum.txt.asceee4:name30:CentOS-7-x86_64-LiveGNOME-180412:piece lengthi524288e6:pieces52980:'
];

function getName(str) {
  const match = str.match(/:name(\d+):(.+)$/);
  if (!match) {
    return console.log('No name found');
  }
  const [, length, rest] = match;
  console.log(
    rest.slice(0, length)
  );
}
input.forEach(getName)

如果字符串中可能包含行终止符,则(.+)$将不起作用,因为.与换行符不匹配-请删除$,或者如果可能要匹配的名称中的换行符,请改用([\s\S]+)$来匹配任何字符,包括换行符。