gpt4 book ai didi

c++ - 正则表达式查找 mp3 html 文件

转载 作者:行者123 更新时间:2023-11-30 17:21:08 27 4
gpt4 key购买 nike

我正在尝试匹配 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;
}

最佳答案

因为.*默认是贪婪的。它尽可能地贪婪地匹配所有字符。

std::regex re("(http://)(.*?)([.]mp3\" )");

如果您不想包含 "<space>,请使用以下正则表达式最后。

std::regex re("(http://)(.*?)[.]mp3(?=\" )");

关于c++ - 正则表达式查找 mp3 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28382402/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com