gpt4 book ai didi

c++ 搜索不匹配

转载 作者:太空宇宙 更新时间:2023-11-04 11:34:50 24 4
gpt4 key购买 nike

我很困惑为什么使用 C++ 进行的看似简单的 RE 搜索无法正常工作。代码是:

    // cliopts.infiledir.c_str() == "./samples/"
DIR* infiledir = opendir(cliopts.infiledir.c_str());
struct dirent* dirent = nullptr;
std::regex infilere (".*");
if (infiledir != nullptr) {
while ((dirent = readdir(infiledir)) != nullptr) {
std::string filename (dirent->d_name);
if (std::regex_search(filename.begin(), filename.end(), infilere)){
std::cout << "MATCH " << filename << "\n";
} else {
std::cout << "----- " << filename << "\n";
}
}
}

输出是:

----- .
----- ..
----- lena.jpg
----- sample0001.jpg
----- sample0002.jpg
----- sample0003.jpg
----- sample0004.jpg
----- sample0005.jpg

我要为自己看不到什么而自责?


请注意,这是使用 g++ -std=c++0x 在 Arch GNU/Linux 上构建和运行的。

最佳答案

GNU 的 libstdc++<regex> 根本不起作用(直到 4.9 )。安装g++-4.9libc++或使用 boost::regex ... 很遗憾。这个程序:

#include <iostream>
#include <string>
#include <regex>
#include <dirent.h>

using namespace std;

int main() {
DIR* infiledir = opendir(".");
struct dirent* dirent = nullptr;
std::regex infilere (".*");
if (infiledir != nullptr) {
while ((dirent = readdir(infiledir)) != nullptr) {
std::string filename (dirent->d_name);
if (std::regex_search(filename.begin(), filename.end(), infilere)){
std::cout << "MATCH " << filename << "\n";
} else {
std::cout << "----- " << filename << "\n";
}
}
}
}

它的输出:

PROMPT$  g++-4.8 -std=c++11 -o tmz tmz.cc && ./tmz
----- x.txt
----- plik1.txt~
----- tmz.cc
----- tmz
----- Makefile
----- ..
----- tmz.s
----- plik1.txt
----- .
PROMPT$ g++-4.9 -std=c++11 -o tmz tmz.cc && ./tmz
MATCH x.txt
MATCH plik1.txt~
MATCH tmz.cc
MATCH tmz
MATCH Makefile
MATCH ..
MATCH tmz.s
MATCH plik1.txt
MATCH .
PROMPT$ g++-libc++ -std=c++11 -o tmz tmz.cc && ./tmz
MATCH x.txt
MATCH plik1.txt~
MATCH tmz.cc
MATCH tmz
MATCH Makefile
MATCH ..
MATCH tmz.s
MATCH plik1.txt
MATCH .
PROMPT$ clang++ -std=c++11 -o tmz tmz.cc && ./tmz
MATCH x.txt
MATCH plik1.txt~
MATCH tmz.cc
MATCH tmz
MATCH Makefile
MATCH ..
MATCH tmz.s
MATCH plik1.txt
MATCH .
PROMPT$ clang++-libc++ -std=c++11 -o tmz tmz.cc && ./tmz
MATCH x.txt
MATCH plik1.txt~
MATCH tmz.cc
MATCH tmz
MATCH Makefile
MATCH ..
MATCH tmz.s
MATCH plik1.txt
MATCH .

关于c++ <regex> 搜索不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23302655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com