我试图在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) 可能重复:
与c ++ 11正则表达式不匹配
我boost::regex以前用过一些东西和一些我想要使用的新东西,std::regex直到我注意到以下不一致 - 所以问题是哪一个是正确的?
#include <iostream>
#include <regex>
#include <string>
#include <boost/regex.hpp>
void test(std::string prefix, std::string str)
{
std::string pat = prefix + "\\.\\*.*?";
std::cout << "Input : [" << str << "]" << std::endl;
std::cout << "Pattern : [" << pat << "]" << std::endl;
{
std::regex r(pat);
if (std::regex_match(str, r))
std::cout << "std::regex_match: true" << std::endl;
else
std::cout << "std::regex_match: false" << std::endl;
if (std::regex_search(str, r))
std::cout << "std::regex_search: true" << …Run Code Online (Sandbox Code Playgroud) 我一直在使用C++中的正则表达式,但遇到了一些错误:
这是我的剧本
#include <iostream>
#include <regex>
using namespace std;
string input(string prompt)
{
cout << prompt;
string str;
cin >> str;
return str;
}
int main() {
string str;
while (true) {
str = input("Enter some text: ");
regex e("([:w:])+", regex_constants::icase);
bool match = regex_match(str, e);
cout << (match? "Matched" : "Not matched") << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译它并运行(g++ -std=c++11 test.cpp && ./a.out)时,我收到以下错误:
Enter some text: abcde
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted (core …Run Code Online (Sandbox Code Playgroud) 可能重复:
关于正则表达式的gcc4.7错误吗?
我用"g ++ test.cpp -std = gnu ++ 0x"编译下面的代码.编译是成功的,但是当我运行./a.out时,它会给出错误,就像我不明白它为什么会发生一样.我的操作系统是Mint.
错误:"在抛出'std :: regex_error'的实例后调用终止what():regex_error Aborted(core dumped)"
Code:
// regex_match example
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main ()
{
string s ("this subject has a submarine as a subsequence");
regex e("sub[a-z]"); // matches words beginning by "sub"
smatch m;
return 0;
}
Run Code Online (Sandbox Code Playgroud)