使用C ++ RegEx进行后向查找的替代方法是什么? [英] What is an alternative for lookbehind with C++ RegEx?

查看:68
本文介绍了使用C ++ RegEx进行后向查找的替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下模式:

(?<=<)(?<!>).*?q.*?(?!<)(?=>)

它使用正向和负向先行和向后查找来匹配包含在匹配括号中的文字q.

It uses positive and negative lookahead and lookbehind to match the literal q that is enclosed in matching brackets.

std::regex不支持向后搜索.那么什么是更好的选择?

std::regex does not support lookbehind. So what would be a good alternative?

推荐答案

请注意,(?<=<)(?<!>)等于(?<=<)(由于必须在当前位置的左侧紧接一个<,所以>)和(?!<)(?=>)等于(?=>)(此处适用相同的逻辑,因为>必须紧接在右边,而不会出现任何<).第一个.*?将不匹配可能的最短子字符串,它将从字面上找到第一个q的方式,后跟任何0+字符直到第一个>.因此,即使在支持后向支持的引擎中,该模式也几乎对您不起作用.

Note that (?<=<)(?<!>) is equal to (?<=<) (since a < is required immediately to the left of the current location, there cannot be any >) and (?!<)(?=>) is equal to (?=>) (same logic applies here, as > must be immediately to the right, there won't be any <). The first .*? will not match the shortest substring possible, it will literally find its way to the first q that is followed with any 0+ chars up to the first >. So, the pattern is hardly working for you even in the lookbehind-supporting engine.

我宁愿使用<([^<>q]*q[^<>]*)>正则表达式和捕获组,并在表达式的开始/结尾使用文字消耗<>符号:

I'd rather use <([^<>q]*q[^<>]*)> regex with a capturing group and literal consuming < and > symbols at the start/end of the expression:

std::regex r("<([^<>q]*q[^<>]*)>");
std::string s = "<adqsdq<><abc>5<abq>6<qaz> <hjfffffffk>";
for(std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), r);
                         i != std::sregex_iterator();
                         ++i)
{
    std::cout << (*i).str(1)  << srd::endl;
}

请参见 C ++演示

输出:abqqaz

这篇关于使用C ++ RegEx进行后向查找的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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