我正在尝试匹配html
中的所有mp3链接预期产出
http://mp3cofe.com/ariana-grande-weeknd-love-me-harder-andreevskiy-remix.mp3
http://mp3cofe.com/listen/52d-remix.mp3
获得输出
http://mp3cofe.com/ariana-grande-weeknd-love-me-harder-andreevskiy-remix.mp3" rel="nofollow" target="_blank" style="color:green;">Download</a</div><a href="http://mp3cofe.com/listen/52d-remix.mp3"
码
#include <iostream>
#include <string>
#include <regex>
int main(){
std::string subject("<a href=\"http://mp3cofe.com/ariana-grande-weeknd-love-me-harder-andreevskiy-remix.mp3\" rel=\"nofollow\" target=\"_blank\" style=\"color:green;\">Download</a></div><a href=\"http://mp3cofe.com/listen/52d-remix.mp3\" rel=\"nofollow\" target=\"_blank\" style=\"color:green;\">Download</a> ");
std::regex re("(http:\/\/)(.*)(\.mp3\"\ )");
std::sregex_iterator next(subject.begin(), subject.end(), re);
std::sregex_iterator end;
while (next != end) {
std::smatch match = *next;
std::cout << match.str() << "\n";
next++;
}
return 0;
}
答案 0 :(得分:0)
因为默认情况下.*
是贪婪的。它尽可能地贪婪地匹配所有角色。
std::regex re("(http://)(.*?)([.]mp3\" )");
如果您不想在最后添加"<space>
,请使用以下正则表达式。
std::regex re("(http://)(.*?)[.]mp3(?=\" )");