user4016367 user4016367 - 1 month ago 9
C++ Question

c++ regex get folder from a file path

I have a file name like this

/mnt/opt/storage/ssd/subtitles/8/vtt/2011022669-5126858992107.vtt


how to replace the file name with
*
using regex so I get

/mnt/opt/storage/ssd/subtitles/8/vtt/*
?

I know the simple for loop split or
boost::filesystem
approach, I'm looking for a
regex_replace
approach.

Answer

You don't need regexp for this:

string str = "/mnt/opt/storage/ssd/subtitles/8/vtt/2011022669-5126858992107.vtt";
auto lastSlash = str.find_last_of('/');
str.replace(str.begin() + lastSlash + 1, str.end(), "*");
Comments