需要正则表达式帮助来提取十六进制值

时间:2019-03-21 09:24:37

标签: javascript regex

在这个例子中,我想提取没有std::map<std::string, std::list<std::string>> mapex; mapex["a"] = { "1", "2", "3", "4" }; mapex["b"] = { "5", "6", "7" }; for (const auto &[k, v] : mapex) { std::cout << "m[" << k.c_str() << "] ="; for (const auto &s : v) std::cout << " " << s.c_str(); std::cout << std::endl; }

的哈希值
:

期望得到:

  • 0478880046C0A5007A46641270EE531389FF419ADE21E30D9636E07F0A928169

因此,它必须找到以a=foo:val a=bar:val a=hash:sha-256 04:78:88:00:46:C0:A5:00:7A:46:64:12:70:EE:53:13:89:FF:41:9A:DE:21:E3:0D:96:36:E0:7F:0A:92:81:69 a=baz:val 开头的行,并在此之后提取所有十六进制值

2 个答案:

答案 0 :(得分:2)

您可以使用String#matchString#replace来获得结果。其中String#match方法可用于从行中获取字符串,而String#replace方法可用于从字符串中删除:

var res = str
        // get the string after sha-256 in capturing group
        .match(/a=hash:sha-256\s+(.*)/)[1]
        // apply replace to remove all :
        .replace(/:/g, '');

var str = `a=foo:val
a=bar:val
a=hash:sha-256 04:78:88:00:46:C0:A5:00:7A:46:64:12:70:EE:53:13:89:FF:41:9A:DE:21:E3:0D:96:36:E0:7F:0A:92:81:69
a=baz:val`;

var res = str.match(/a=hash:sha-256\s+(.*)/)[1].replace(/:/g, '');

console.log(res)

答案 1 :(得分:0)

您可以使用splitstartsWith

做类似的事情

let str = `a=foo:val
a=bar:val
a=hash:sha-256 04:78:88:00:46:C0:A5:00:7A:46:64:12:70:EE:53:13:89:FF:41:9A:DE:21:E3:0D:96:36:E0:7F:0A:92:81:69
a=baz:val`;

let output = str.split('\n')
             .find(a => a.startsWith('a=hash:sha-256'))
             .replace(/a=hash:sha-256|:/g, '');

console.log(output)