c++输入内容选取文本中的链接
时间: 2023-07-08 15:04:22 浏览: 48
要从文本中选取链接,可以使用正则表达式来匹配符合链接格式的字符串。以下是一个示例程序,它会从输入的文本中选取所有符合链接格式的字符串:
```c++
#include <iostream>
#include <regex>
#include <string>
int main() {
std::string input;
std::getline(std::cin, input);
std::regex link_regex("(http|https)://[a-zA-Z0-9./?=_-]+");
auto words_begin = std::sregex_iterator(input.begin(), input.end(), link_regex);
auto words_end = std::sregex_iterator();
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
std::smatch match = *i;
std::cout << match.str() << std::endl;
}
return 0;
}
```
在上面的代码中,我们使用了 C++11 中的正则表达式库。首先,我们从标准输入中读取一行文本。然后,我们定义了一个正则表达式 `link_regex`,它可以匹配以 http 或 https 开头、后面跟着一些字母、数字、斜线、问号、下划线、连字符或等号的字符串。接下来,我们使用 `std::sregex_iterator` 来遍历文本中所有符合链接格式的字符串,并将它们输出到标准输出中。
你可以根据自己的需要修改正则表达式来匹配不同格式的链接。