本文介绍了编号回引用,后跟文字编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行正则表达式替换.我似乎无法弄清的具体问题是,在第二次反向引用之后,我有一个字符串文字数字(数字"1").使用MS Visual Studio 2012(C ++控制台项目...而不是.NET),它不起作用.我认为是因为它将我的后向引用作为$ 21而不是$ 2.我已经尝试过各种语法,但无法提出有用的建议!

I'm trying to perform a regular expression substitution. The specific issue I can't seem to figure out is that following my 2nd backreference, I have a string literal number (the numeral one). Using MS Visual Studio 2012 (C++ console project...not .NET), it doesn't work. I assume because it takes my backreference as $21, instead of $2. I've tried various syntax but can't come up with something that works!

std::string input = "my_variable_name_iei_lo1";
std::string regx = "(\\w+)iei_(lo_hi)1";
std::string sub = "$1ied_$21";

std::regex rx(regx);

std::string result = std::regex_replace(input, rx, sub);

// result is "my_variable_name_ied_"
// should be "my_variable_name_ied_lo1"

我尝试了各种指定反向引用的方法:

I've tried various methods of specifying the backreference:

std::string sub = "$1ied_${2}1";

// result is "my_variable_name_ied_${2}1"
// should be "my_variable_name_ied_lo1"

其他情况给我带来语法错误,包括尝试使用命名的捕获组,但随后会发现该错误不再受支持.如此接近我的答案,但仍然遥不可及!

Other things give me syntax errors, including trying to use named capture groups, but then read that that is no longer supported. So close to my answer, but still so far away!

推荐答案

Gunn 找到的解决方案:

Using $021 worked! I knew about the two digit limitation (99 capture groups)
but for whatever reason, never thought to try 01, 02, etc. So having the substitution
string be "$01ied_$021" gives me the result I was looking for. Thanks!

这篇关于编号回引用,后跟文字编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 05:35