gpt4 book ai didi

c++ - Regex LookAround 在 python 中工作,但在 C++ 中不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:01 26 4
gpt4 key购买 nike

我对 Python 中的 Regex 工作有相当好的理解,但是用于环视工作的相同 regex 字符串在 C++ 中不起作用。我用以下 example 测试了这个这看起来不错。但是,对于以下 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++ 中没有任何直接的方法可以做到这一点吗?

最佳答案

试试这个作为入门,这会让你对 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;
}

关于c++ - Regex LookAround 在 python 中工作,但在 C++ 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40624291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com