Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用boost::regex从目录获取包含某些正则表达式的文件名时出现意外输出_C++_Boost Regex - Fatal编程技术网

C++ 使用boost::regex从目录获取包含某些正则表达式的文件名时出现意外输出

C++ 使用boost::regex从目录获取包含某些正则表达式的文件名时出现意外输出,c++,boost-regex,C++,Boost Regex,我刚刚做了一个函数findFile来查找目录dir\u name中是否有一个具有某种模式的文件file\u name\u regex。就在这里测试一下 应打印出: a.out 但它会产生意想不到的结果: .out 它基于 我还对其进行了更改,以在以下方面进行简单测试: #包括 #包括 #包括 int main() { std::字符串文本(“a.out”); const char*pattern=“a.out”; boost::regex ip_regex(模式); sregex_迭代器it

我刚刚做了一个函数
findFile
来查找目录
dir\u name
中是否有一个具有某种模式的文件
file\u name\u regex
。就在这里测试一下

应打印出:

a.out
但它会产生意想不到的结果:

.out
它基于

我还对其进行了更改,以在以下方面进行简单测试:

#包括
#包括
#包括
int main()
{
std::字符串文本(“a.out”);
const char*pattern=“a.out”;
boost::regex ip_regex(模式);
sregex_迭代器it(text.begin()、text.end()、ip_regex);
boost::sregex_迭代器end;
for(;it!=end;++it){
std::cout str()str();或类似的东西
}
}
它打印出预期的单词
a.out


那么我的代码怎么了?由于指针悬空,您得到了UB。临时的
itr->path().filename().string()
在以下语句末尾被销毁:

        boost::sregex_iterator it(itr->path().filename().string().begin(),
                               itr->path().filename().string().end(), 
                               file_regex);
所以
begin()
end()
现在指向垃圾

您需要将临时
字符串提升到单独的变量中以延长其生命周期:

        std::string s = itr->path().filename().string();
        boost::sregex_iterator it(s.begin(), s.end(), file_regex);

你真的需要更仔细地研究正则表达式。点
具有特殊含义。你确定你想要正则表达式(这常常是过度的)而不使用吗?@某个程序员,是的,我的代码库是C++,名字模式可能不是那么简单。实际上,我想用基本的正则表达式“^test”*”模式捕获文件。
itr->path().filename().string()。在这种情况下,比较(0,5,“test”
就是您所需要的。另外,还有一个内置的std::regex,不需要使用boost。@rustyx,是的,它是。好建议-:)。boost主要用于
boost::filesystem
(我们的代码库支持C++11,但std::filesystem支持C++17)。我也看到它的正则表达式,所以我想为什么不写一个更一般的函数…噢,std::regex也支持C++11。
“test.*
是一种全局模式,而不是正则表达式。您需要将其转换为正则表达式。对于这种简单模式,请查看前导的五个字符的子字符串是否等于
”test_“
。我多次运行该程序,但输出总是
。out
在Coliru中没有任何崩溃,对此有什么解释吗?UB表示在编写应用程序时无法预测结果,它并不意味着随机行为。在您的案例中,没有影响行为的外部因素,因此问题是100%可重复的。如果在不同的平台上重新编译或使用不同的优化标志,则可能会发生更改。阅读更多信息。
#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
    std::string text("a.out");
    const char * pattern = "a.out";    
    boost::regex ip_regex(pattern);

    boost::sregex_iterator it(text.begin(), text.end(), ip_regex);
    boost::sregex_iterator end;
    for (; it != end; ++it) {
        std::cout << it->str() << "\n";
        // v.push_back(it->str()); or something similar     
    }
}
        boost::sregex_iterator it(itr->path().filename().string().begin(),
                               itr->path().filename().string().end(), 
                               file_regex);
        std::string s = itr->path().filename().string();
        boost::sregex_iterator it(s.begin(), s.end(), file_regex);