![](/img/trans.png)
[英]How can I use regex in C++ to extract text including spaces between parenthesis
[英]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.