-
c++正则表达式匹配所有文件
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
// 匹配文件或文件夹
string str("^[a-zA-Z]:([\\\\/][^\\s\\\\/:*?<>\"|][^\\\\/:*?<>\"|]*)*([/\\\\])?$");
// 仅匹配文件
//string str("^[a-zA-Z]:([\\\\/][^\\s\\\\/:*?<>\"|][^\\\\/:*?<>\"|]*)+$");
string fileName("C:\\s");
try {
regex reg(str);
if (std::regex_match(fileName, reg)) {
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
}
catch (regex_error e) {
cout << e.what() << "\ncode: " << e.code();
}
system("pause");
return 0;
}