c++获取文本中的url链接
时间: 2024-04-27 13:24:20 浏览: 16
可以使用正则表达式来获取文本中的URL链接。以下是一个示例代码,可以使用C++11标准的regex库来实现:
```c++
#include <iostream>
#include <regex>
#include <string>
int main() {
std::string text = "Lorem ipsum dolor sit amet, https://www.google.com consectetur adipiscing elit, http://www.example.com sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
std::regex url_regex(R"(https?://\S+)");
auto words_begin = std::sregex_iterator(text.begin(), text.end(), url_regex);
auto words_end = std::sregex_iterator();
std::cout << "URLs found:" << std::endl;
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
std::smatch match = *i;
std::string match_str = match.str();
std::cout << match_str << std::endl;
}
return 0;
}
```
该代码将输出以下内容:
```
URLs found:
https://www.google.com
http://www.example.com
```
该正则表达式可以匹配以http或https开头的任何字符串,直到遇到下一个空格或其他空白字符为止。请注意,该代码只是一个示例,实际应用中可能需要更复杂的正则表达式来匹配不同的URL格式。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)