jeudi 29 octobre 2015

how to isolate backrefernce in c++ regex_replace

I am using the following code:

#include <iostream>
#include <string>
#include <regex>
#include <iterator>

int main ()
{
  std::string s ("there is a subsequence in the string\n");
  std::regex e ("\\b(a )(subsequence)([^ ]*)");
  // with flags:
  std::cout << std::regex_replace (s,e,"$11digit$3",std::regex_constants::format_default);
  std::cout << std::endl;

  return 0;
}

Where $1 and $2 are the back-references. The output of the above command:

there is digit in the string

But the output should be:

there is a 1digit in the string

It's obvious that $11 is taking it as 11th backreference, but I just want to append a 1 at the end of the backreference $1. How do I isoloate the $1 so that I can put a digit after it.

I have tried to isolate it with ${1}, but it didn't work.

Aucun commentaire:

Enregistrer un commentaire