#include <iostream>
#include <regex>
int main() {
std::string s = "{\"|1|\":\"A\",\"|2|\":\"B\",\"|37|\":\"4234235\",\"|4|\":\"C\"}";
std::regex regex("37\\|\\\\\":\\\\\"\\K\\d*");
std::smatch m;
regex_search(s, m, regex);
std::cout << "match: " << m.str(1) << std::endl;
return 0;
}
为什么与值不匹配4234235
?
在这里测试正则表达式:https://regex101.com/r/A2cg2P/1 https://regex101.com/r/A2cg2P/1它确实匹配。
您的在线正则表达式测试是错误的,因为您的实际文本是{"|1|":"A","|2|":"B","|37|":"4234235","|4|":"C"}
,你可能会看到你的正则表达式不匹配 https://regex101.com/r/OvAxPt/1.
此外,您正在使用 ECMAScript 正则表达式风格std::regex
,但您的正则表达式符合 PCRE。例如。 ECMAScript 正则表达式不支持\K
匹配重置运算符。
你需要一个"\|37\|":"(\d+)
正则表达式,请参阅正则表达式演示 https://regex101.com/r/OvAxPt/2. Details:
-
"\|37\|":"
- 字面意思"|37|":"
text
-
(\d+)
- 第 1 组:一位或多位数字。
See C++ 演示 https://ideone.com/MtZoHM:
#include <iostream>
#include <regex>
int main() {
std::string s = "{\"|1|\":\"A\",\"|2|\":\"B\",\"|37|\":\"4234235\",\"|4|\":\"C\"}";
std::cout << s <<"\n";
std::regex regex(R"(\|37\|":"(\d+))");
std::smatch m;
regex_search(s, m, regex);
std::cout << "match: " << m.str(1) << std::endl;
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)