Numbered back reference followed by literal number

一笑奈何 提交于 2019-12-19 08:31:03

问题


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!


回答1:


Solution found by 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!


来源:https://stackoverflow.com/questions/26107847/numbered-back-reference-followed-by-literal-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!