这是MCVE:
#include <iostream>
#include <regex>
std::string s()
{
return "test";
}
int main()
{
static const std::regex regex(R"(\w)");
std::smatch smatch;
if (std::regex_search(s(), smatch, regex)) {
std::cout << smatch[0] << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译很好:
$ clang ++ -std = c ++ 11 main.cpp
但不是:
$ clang ++ -std = c ++ 14 main.cpp
后一种情况下的错误消息(使用-std = c ++ 14):
main.cpp:14:9: error: call to deleted function 'regex_search'
if (std::regex_search(s(), smatch, regex)) {
^~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:5998:1: note:
candidate function [with _ST = std::__1::char_traits<char>, …Run Code Online (Sandbox Code Playgroud) 我希望能够解决这样的问题:Getting std::ifstream to handle LF, CR, and CRLF? 其中 anistream需要通过复杂的分隔符进行标记;这样标记化的唯一方法istream是:
istream一次读取一个字符正则表达式非常擅长用复杂的分隔符标记字符串:
string foo{ "A\nB\rC\n\r" };
vector<string> bar;
// This puts {"A", "B", "C"} into bar
transform(sregex_iterator(foo.cbegin(), foo.cend(), regex("(.*)(?:\n\r?|\r)")), sregex_iterator(), back_inserter(bar), [](const smatch& i){ return i[1].str(); });
Run Code Online (Sandbox Code Playgroud)
但我不能regex_iterator在 a 上使用 a istream:( 我的解决方案是吸食istream然后运行regex_iterator它,但吸食步骤似乎是多余的。
是否存在istream_iterator和 的邪恶组合regex_iterator,或者如果我想要它,我必须自己编写它吗?