我试图在C++ 11代码中使用std :: regex,但看起来支持有点儿错误.一个例子:
#include <regex>
#include <iostream>
int main (int argc, const char * argv[]) {
std::regex r("st|mt|tr");
std::cerr << "st|mt|tr" << " matches st? " << std::regex_match("st", r) << std::endl;
std::cerr << "st|mt|tr" << " matches mt? " << std::regex_match("mt", r) << std::endl;
std::cerr << "st|mt|tr" << " matches tr? " << std::regex_match("tr", r) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
st|mt|tr matches st? 1
st|mt|tr matches mt? 1
st|mt|tr matches tr? 0
Run Code Online (Sandbox Code Playgroud)
当使用gcc(MacPorts gcc47 4.7.1_2)4.7.1编译时,使用
g++ *.cc -o test -std=c++11 …Run Code Online (Sandbox Code Playgroud) 为什么在g ++(Debian 4.6.3-1)4.6.3或clang version 3.2(trunk 159457)中找不到匹配项
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
string line("test");
regex pattern("test",regex_constants::grep);
smatch result;
bool ret(false);
ret = regex_search(line,result,pattern);
cout << boolalpha << ret << endl;
cout << result.size() << endl;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
产量
false
0
Run Code Online (Sandbox Code Playgroud)