首页 > 解决方案 > 使用 std::regex 或 boost::regex 库匹配所有以“.txt”结尾的文件的正则表达式是什么?

问题描述

我正在浏览某个目录并想打印所有以 .txt 结尾的文件。此代码不起作用。
ft::FilePath 文件路径;std::filesystem::recursive_directory_iterator iter(m_rootDirectoryPath);

    const std::regex e("*.txt");

    for(;iter != std::filesystem::end(iter); ++iter)
    {
        if(std::regex_match(iter->path().string(),e))
        {
            const std::string& p = iter->path().string();
            uint64_t pos = p.find(m_rootDirectoryPath.string());
            filePath.set_path(p.substr(pos + m_rootDirectoryPath.string().size()));
            writer->Write(filePath);
        }
    }

匹配所有以 .txt 结尾的文件的正则表达式是什么?(你必须假设我使用 std::regex 来匹配模式)

标签: c++regexstd

解决方案


".*\\.txt$"

您的表达式是 shell 中使用的一个 glob。


推荐阅读