为什么这个正则表达式在STL世界中是错误的?
(?<=^EXTINF:)*(\d+\.\d+)
“?&lt; =”部分抛出异常。为什么???
更新
const char * const pm3u =
{
“#EXTM3U \ n”“#EXT-X-VERSION:3 \ n”
“#EXT-X-MEDIA-SEQUENCE:153741 \ n”
“#EXT-X-ALLOW-CACHE:NO \ n”
“#EXT-X-TARGETDURATION:11 \ n”
“#EXTINF:10.005333,\ n”
“/RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-58-29.ts\n”
“#EXTINF:9.984000,\ n”
“/RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-58-39.ts\n”
“#EXTINF:10.005333,\ n”
“/RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-58-49.ts\n”
“#EXTINF:10.005333,\ n”
“/RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-58-59.ts\n”
“#EXTINF:10.005333,\ n”
“/RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-59-09.ts\n”
“#EXTINF:9.984000,\ n”
“/ RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-59-19.ts\n”
};int main()
{
std :: regex regExpression(“(#EXTINF:)(\\ d +。\\ d +)*”);
std :: smatch regExMatch;
const std :: string str(pm3u);
bool b = std :: regex_match(str.begin(),str.end(),regExMatch,regExpression);
返回0;
}
答案 0 :(得分:0)
如果
,您的正则表达式可以修复^
替换为(?:^|\n)
以匹配行的开头,而不仅仅是字符串的开头std::sregex_iterator
来匹配多个匹配而不是std::regex_match
只匹配整个字符串(即模式必须与整个字符串匹配)#
之前添加EXTINF
,因为您的预期匹配始于#EXTINF
。使用
const std::regex rx(R"((?:^|\n)#EXTINF:(\d+\.\d+),[\r\n]+(.*))");
std::string test = "#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-MEDIA-SEQUENCE:153741\n#EXT-X-ALLOW-CACHE:NO\n#EXT-X-TARGETDURATION:11\n#EXTINF:10.005333,\n/RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-58-29.ts\n#EXTINF:9.984000,\n/RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-58-39.ts\n#EXTINF:10.005333,\n/RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-58-49.ts\n#EXTINF:10.005333,\n/RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-58-59.ts\n#EXTINF:10.005333,\n/RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-59-09.ts\n#EXTINF:9.984000,\n/RTS_1_009/audio/2018-03-16-H13/audio-2018-03-16-13-59-19.ts";
for(std::sregex_iterator i = std::sregex_iterator(test.begin(), test.end(), rx);
i != std::sregex_iterator();
++i)
{
std::smatch m = *i;
std::cout << "Part 1:" << m[1].str() << "\nPart 2:" << m[2].str() << std::endl;
}
正则表达式是
(?:^|\n)#EXTINF:(\d+\.\d+),[\r\n]+(.*)