C++ c++;regex从文件路径获取文件夹
我有一个这样的文件名C++ c++;regex从文件路径获取文件夹,c++,regex,C++,Regex,我有一个这样的文件名 /mnt/opt/storage/ssd/subtitles/8/vtt/2011022669-5126858992107.vtt 如何使用正则表达式将文件名替换为*,以便 /mnt/opt/storage/ssd/subtitles/8/vtt/* 我知道简单的for循环拆分或boost::filesystem方法,我正在寻找regex\u替换方法。试试这种模式 (([\w+\-])+)(?=(\.\w{3})) 在记事本++中测试 (?=())它看起来很有趣。因此,只
/mnt/opt/storage/ssd/subtitles/8/vtt/2011022669-5126858992107.vtt
如何使用正则表达式将文件名替换为*
,以便
/mnt/opt/storage/ssd/subtitles/8/vtt/*
我知道简单的for循环拆分或boost::filesystem
方法,我正在寻找regex\u替换方法。试试这种模式
(([\w+\-])+)(?=(\.\w{3}))
在记事本++中测试
(?=())它看起来很有趣。因此,只有当.xxx或.xx格式的扩展名(.\w{2,3))在这个组之后时,它才会匹配([\w+-])+。
在C++中,你必须只替换组到某物
替换(字符串,1,*)-我不知道C++替换函数,只是假设。< /P>
$1、$2、$3…它的组号,在本例中为$1它的([\w+-])+)。您不需要regexp:
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(), "*");
以下是使用regexp_replace的解决方案:
std::string path=“/mnt/opt/storage/ssd/subtitles/8/vtt/2011022669-5126858992107.vtt”;
标准::正则表达式re(R“(\/[^\/]*?\..+$)”;
std::我能建议尝试而不是仅仅看一眼吗?到目前为止,你有没有想出任何代码?
std::string path = "/mnt/opt/storage/ssd/subtitles/8/vtt/2011022669-5126858992107.vtt";
std::regex re(R"(\/[^\/]*?\..+$)");
std::cout << path << '\n';
std::cout << std::regex_replace(path, re, "/*") << '\n';