重叠匹配的C ++正则表达式 [英] C++ regex for overlapping matches

查看:114
本文介绍了重叠匹配的C ++正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串'CCCC',我想在其中匹配'CCC',并且有重叠。

I have a string 'CCCC' and I want to match 'CCC' in it, with overlap.

我的代码:

...
std::string input_seq = "CCCC";
std::regex re("CCC");
std::sregex_iterator next(input_seq.begin(), input_seq.end(), re);
std::sregex_iterator end;
while (next != end) {
    std::smatch match = *next;
    std::cout << match.str() << "\t" << "\t" << match.position() << "\t" << "\n";
    next++;
}
...

但是这只会返回

CCC 0 

并跳过了我所需的 CCC 1 解决方案。

and skips the CCC 1 solution, which is needed for me.

我读到了非贪婪的'?'匹配,但我无法使其正常工作

I read about non-greedy '?' matching, but I could not make it work

推荐答案

您的正则表达式可以放在捕获括号中,并用正号包裹展望。

Your regex can be put into the capturing parentheses that can be wrapped with a positive lookahead.

也要使其在Mac上运行,请确保正则表达式匹配(并因此消耗 )单个通过在每个匹配项中放置来匹配char。(或-也要匹配换行符char- [\s\S]

To make it work on Mac, too, make sure the regex matches (and thus consumes) a single char at each match by placing a . (or - to also match line break chars - [\s\S]) after the lookahead.

然后,您将需要修改代码以获取第一个捕获组值,如下所示:

Then, you will need to amend the code to get the first capturing group value like this:

#include <iostream>
#include <regex>
#include <string>
using namespace std;

int main() {
    std::string input_seq = "CCCC";
    std::regex re("(?=(CCC))."); // <-- PATTERN MODIFICATION
    std::sregex_iterator next(input_seq.begin(), input_seq.end(), re);
    std::sregex_iterator end;
    while (next != end) {
        std::smatch match = *next;
        std::cout << match.str(1) << "\t" << "\t" << match.position() << "\t" << "\n"; // <-- SEE HERE
        next++;
    }
    return 0;
}

请参见 C ++演示

输出:

CCC     0   
CCC     1   

这篇关于重叠匹配的C ++正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆