繁体   English   中英

我将如何使用正则表达式来忽略字符串中的多个括号?

[英]How would I use regex to ignore multiple parenthesis inside a string?

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

(字符串的输入)

插入((空中客车,A340,295,137),PlaneType)
INSERT((1050,Meal,N),Flight)
插入((1050,10/5/2021,1/1/2021,19:20,1/1/2002,20:40,8.0),FlightLegInstance)
插入((海、西雅图、华盛顿州)、机场)

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

插入((空中客车,A340,295,137),PlaneType)

我需要抢

“空客,A340,295,137”

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

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

但我不了解如何提取内部数据,因为一旦我有了数据,我就需要将其传递给 function 等。

任何帮助表示赞赏。 谢谢!

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

\(([^)]*)\)

这导致了这个

((空客,A340,295,137)

所以我不确定如何删除前导和尾随括号。

由于我尽可能避免使用正则表达式,因此这里是使用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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM