jeudi 29 septembre 2016

Regex substitute file extension prefix placement doesn't work - replace matches

I'm quite familiar with regex, but I'm not familiar with C++11's std::regex_replace. I spent like more than an hour trying to figure out how to replace a simple file extension, but nothing is working!

What I want to achieve is quite simple. I want to replace myfile.pdf with myfile.txt. So I wrote my regex to match files that end with .pdf:

std::regex regex("^.*\\.(pdf)$")

And following a reference and following a cheat-sheet, I want to see the prefix (whatever comes before a match), so I use:

std::string myStr = std::regex_replace(std::string("HiThere.pdf"), regex, std::string("$`"))
std::cout<< myStr <<std::endl;

And I get an empty string! This happens, although I get a match! To show that I'm getting a match, simply replace "$`" with "$1", and you'll see the first match replacing the whole string.

One side note, according to all references, $0 should show the whole string, but it doesn't!

All I want to achieve is to replace that match with ".txt". What am I doing wrong here?

Aucun commentaire:

Enregistrer un commentaire