我对Python中的Regex工作有相当好的理解,但是用于查找工作的同一个regex字符串在C++中不起作用。我已经用下面的示例测试了这个,这个看起来很好。但是,对于下面的C++代码片段,相同的字符串显示为false。
#include <iostream>
#include <regex>
using namespace std;
int main()
{
const char* rejectReason = "Failed to execute SQL. Error=ORA-00936: missing expression";
regex rgx(".+?(?=(ORA-([0-9]{5}):))");
cout<<regex_match(rejectReason, rgx)<<endl;
return 0;
}我对C++有点陌生,许多参考资料表明,展望是有效的,但在C++中不起作用,也没有提到这个查找。那么,在C++中难道没有任何直接的方法可以做到这一点吗?
发布于 2016-11-16 05:05:12
尝试作为初学者,这将为您提供一个基本的regex库的工作原理。
#include <iostream>
#include <regex>
using namespace std;
int main()
{
string rejectReason = "Failed to execute SQL. Error=ORA-00936: missing expression";
regex rgx(".*Error=ORA-([0-9]{5}).*$");
if (regex_match(rejectReason, rgx)) {
cout << "String matches" <<endl;
}
smatch match;
string result;
if (regex_search(rejectReason, match, rgx) && match.size() > 1) {
result = match.str(1);
} else {
result = string("");
}
cout << result << endl;
return 0;
}https://stackoverflow.com/questions/40624291
复制相似问题