c++ - Extract all substrings from a string that are between a word and a delimiter using regex c++
问题描述
I have the following query:
std::string query =
"ODR+1"
"DPT+::SFO"
"ARR+::MKE"
"ODR+2"
"DPT+::MKE"
"ARR+::SFO";
I am trying to extract from all the segments starting with ARR
or DPT
the values after ::
. I wrote the following regular expression [DPT|ARR]\+\:\:(.*)
. It worked when I tested it on regex101
When I wrote the following C++ code. I got the following output:
DPT+::SFO'ARR+::MKE'ODR+2'DPT+::MKE'ARR+::SFO'
The output is wrong I really just want to extract SFO and MKO. How can I modify the regex query to just extract these patterns
#include <regex>
#include <iostream>
int main()
{
std::string query =
"ODR+1'"
"DPT+::SFO'"
"ARR+::MKE'"
"ODR+2'"
"DPT+::MKE'"
"ARR+::SFO'";
std::regex regulaExpression("(DPT|ARR).*::(.*)\\'");
std::sregex_iterator iter(query.begin(), query.end(), regulaExpression);
std::sregex_iterator end;
while(iter != end)
{
std::cout << iter->str() << std::endl;
++iter;
}
}
UPDATE
I updated the code:
#include <regex>
#include <iostream>
#include <cstring>
int main()
{
const char *target =
"ODR+1'"
"DPT+::SFO'"
"ARR+::MKE'"
"ODR+2'"
"DPT+::MKE'"
"ARR+::SFO'";
std::regex rgx("(DPT|ARR).*?::(.*?)'");
for(auto it = std::cregex_iterator(target, target + std::strlen(target), rgx);
it != std::cregex_iterator();
++it)
{
std::cmatch match = *it;
std::cout << match[2].str() << '\n';
}
return 0;
}
Now it is allowing me to retrieve the following. Which is exactly what I want. But I dont know why it work.
SFo
MKE
MKE
SFO
It worked by why did I have to use std::cout << match[2].str() << '\n';
解决方案
问题在于您的正则表达式:
(DPT|ARR).*?::(.*?)'
第一部分将获取以or(DPT|ARR)
开头的字符串,但也会保存它,因此结果的第一个元素具有此值。为避免这种情况,请使用非捕获组:DPT
ARR
match[1]
(?: )
第二部分.*?
是问题:它捕获所有内容,包括::
,因此您的正则表达式永远找不到分隔符。您想搜索除 之外的所有内容:
,也可能不搜索'
(以避免错误的部分传播给其他人):(?:[^':]*:)+:
第一部分搜索直到第一个的:
内容,然后检查:
后面是否还有另一个。如果你确定这部分没有 single :
,你可以简化它。
最后,您得到所需的字符串:([^']*)
直到第一个'
. 括号仅用于捕获内容,因此您可以使用match[1]
(?:DPT|ARR)(?:[^':]*:)+:([^']*)
推荐阅读
- node.js - nodejs Angular 应用程序中的无限 xhr-streaming、websocket 和 info 调用
- r - 使用 map_if 将列表加倍为同一类型
- javascript - 您可以使用 v-model 从数组中添加/删除元素吗?
- redis - 嵌套JS“socket.io-redis”:“6.0.1”
- javascript - 通过 Outlook-Addin 访问用户的系统摄像头
- sql - 雪花的“横向视图爆炸”相当于什么?
- c++ - glm 数学库围绕目标旋转
- assembly - ARMv8 程序集,如何将值从 w 寄存器移动到 x 寄存器?
- mysql - 在 SQL ACCESS 中使用 LEFT 或 INNER JOIN 和 WHERE 的问题
- ios - Swift 合并 HTTP 请求