如何使用regex忽略字符串中的多个圆括号



我看到的每个答案似乎都没有一个简单易读的解决方案。这是我正在使用的输入。

(字符串输入(

INSERT((空客,A340295137(,PlaneType(
INSERT((1050,餐点,N(,航班(
INSERT((1050,10/5/2021,1/1/202119:20,1/1/2002,20:40,8.0(,FlightLegInstance(
1INSERT((SEA,Seattle,WA(,AirPort(
2

目标是使用正则表达式捕获嵌套括号内的数据
例如:

INSERT((空客,A340295137(,PlaneType(

我需要获取

"空客A340295137">

我目前正在使用它来搜索我需要的东西

regex_search(input, regex("PlaneType")) //Example

但我不了解如何提取里面的数据,因为一旦我有了数据,我就需要把它传递给一个函数等

感谢您的帮助。谢谢

(更新(
到目前为止我拥有的最好的是:

(([^)]*))

导致这个的原因

((空客,A340295137(

所以我不知道如何删除前括号和后括号。

由于我尽可能避免Regex,下面是使用substr:的示例

std::size_t endPos = str.find("),"); //gets the end of the inner parentheses
std::string sub = str.substr(8, endPos - 8); //substrings to pull inner string out

我不知道你是如何读取这些字符串的,但这里有一个完整的例子来转储提供的样本:

#include <iostream>
#include <vector>
int main()
{
std::vector<std::string> vs;
std::string s1 = "INSERT((AIRBUS,A340,295,137), PlaneType)";
std::string s2 = "INSERT((1050,Meal,N), Flight)";
std::string s3 = "INSERT((1050,10/5/2021,1/1/2021,19:20,1/1/2002,20:40,8.0), FlightLegInstance)";
std::string s4 = "INSERT((SEA,Seattle,WA), AirPort)";
vs.push_back(s1);
vs.push_back(s2);
vs.push_back(s3);
vs.push_back(s4);
for (int i = 0; i < vs.size(); i++) {
std::size_t endPos = vs[i].find("),");
std::string sub = vs[i].substr(8, endPos - 8);
std::cout << sub << std::endl;
}
}

最新更新