在这个例子中,我想提取没有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;
}
:
期望得到:
因此,它必须找到以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
开头的行,并在此之后提取所有十六进制值
答案 0 :(得分:2)
您可以使用String#match
和String#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)
您可以使用split
和startsWith
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)