mercredi 20 mars 2019

C++ format specifier for regex_replace

First time on SO. So sorry if I ask something wrong.

I have some kind of data like this:

{
  key id : 1349439575,
  value uint8Val : 0
}
{
  key id : 1349439540,
  value uint8Val : 0
}

Now I want to replace the number after the key id with its HEX representation but keep other things unchanged. After reading C++ documentation, I can find the match by using the regex like that:

regex re("(\\s*key\\s*id\\s*:\\s*)(\\d+)");
regex_replace(my_data, re, "$1 $2");

The problem is I don't know how to represent the replacement for $2 with format specifier like "%x" to make the equivalent HEX value. That mean, my expected output will be something like this:

{
  key id : 0x506ed057,
  value uint8Val : 0
}
{
  key id : 0x506ed034,
  value uint8Val : 0
}

Do you guy know how to do that? Thanks.

Aucun commentaire:

Enregistrer un commentaire