Mar*_*ark 9 c++ regex string filenames
首先,我将快速描述我对此的动机和实际问题:
我经常处理大批量文件,更具体地说,我发现自己必须按照以下规则重命名:
它们可能都包含单词和数字,但仅限于一组数字正在递增而不是"常量".我需要提取那些只有那些数字并相应地重命名文件.例如:
Foo_1_Bar_2015.jpg
Foo_2_Bar_2015.jpg
Foo_03_Bar_2015.jpg
Foo_4_Bar_2015.jpg
Run Code Online (Sandbox Code Playgroud)
将被重命名:
1.jpg
2.jpg
3.jpg or 03.jpg (The leading zero can stay or go)
4.jpg
Run Code Online (Sandbox Code Playgroud)
所以我们开始的是一个带有std::wstring
对象的向量,用于指定目录中的所有文件名.我敦促你停止阅读3分钟,并在继续我的尝试和问题之前考虑如何处理这个问题.我不希望我的想法在一个方向或另一个方向推动你,我总是发现新的想法是最好的.
现在,我有两种方法可以想到:
1)旧式C字符串操作和比较:
在我看来,这需要解析每个文件名并记住每个数字序列的位置和长度.这很容易存储在矢量中,或者存储在每个文件中.这很好用(基本上使用字符串搜索增加偏移量):
while((offset = filename_.find_first_of(L"0123456789", offset)) != filename.npos)
{
size = filename.find_first_not_of(L"0123456789", offset) - offset;
digit_locations_vec.emplace_back(offset, size);
offset += size;
}
Run Code Online (Sandbox Code Playgroud)
之后我所拥有的是(位置,大小)对的向量,用于文件名中的所有数字,常量(通过使用动机中的定义)与否.
在此之后,随之而来的是混乱,因为您需要交叉引用字符串并找出需要提取的数字.这将随着文件数量(往往是巨大的)而呈指数增长,而不是提到每个字符串中的数字序列数.此外,不是非常易读,可维护或优雅.不行.
2)正则表达式
如果有正则表达式的使用,那就是这个.从第一个文件名中创建一个正则表达式对象,并尝试将其与下一个文件名匹配.成功?即时提取所需数量的能力.失败?添加有问题的文件名作为新的正则表达式对象,并尝试匹配现有的两个正则表达式.冲洗并重复.正则表达式看起来像这样:
Foo_(\d+)_Bar_(\d+).jpg
Run Code Online (Sandbox Code Playgroud)
或分别为每个数字序列创建一个正则表达式:
Foo_(\d+)_Bar_2015.jpg
Foo_1_Bar_(\d+).jpg
Run Code Online (Sandbox Code Playgroud)
其余的是蛋糕.随便继续匹配,在最好的情况下,它可能只需要一次通过!问题是......
我需要知道的是:
1)你能想到任何其他优越的方法来实现这一目标吗?几天来,我一直在撞墙.
2)尽管在第一种方法中字符串操作和矢量构造\破坏的成本可能很大,但与正则表达式对象的成本相比,它可能相形见绌.第二种方法,最坏的情况:与文件一样多的正则表达式对象.对于潜在的数千个文件,这会是灾难性的吗?
3)第二种方法可以根据两种可能性中的一种进行调整:很少有std::regex
对象构造,很多regex_match
调用或者反过来.哪个更昂贵,正则表达式对象的构造或尝试匹配字符串?
对我来说(gcc4.6.2 32位优化O3),手动字符串操作比正则表达式快大约2倍。不值得付出代价。
可运行的完整代码示例(与 boost_system 和 boost_regex 链接,或者如果编译器中已有正则表达式则更改包含):
#include <ctime>
#include <cctype>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
#include <boost/regex.hpp>
using namespace boost;
/*
Foo_1_Bar_2015.jpg
Foo_1_Bar_2016.jpg
Foo_2_Bar_2016.jpg
Foo_2_Bar_2015.jpg
...
*/
vector<string> generateNames(int lenPerYear, int yearStart, int years);
/*
Foo_1_Bar_2015.jpg -> 1_2015.jpg
Foo_7_Bar_2016.jpg -> 7_2016.jpg
*/
void rename_method_string(const vector<string> & names, vector<string> & renamed);
void rename_method_regex(const vector<string> & names, vector<string> & renamed);
typedef void rename_method_t(const vector<string> & names, vector<string> & renamed);
void testMethod(const vector<string> & names, const string & description, rename_method_t method);
int main()
{
vector<string> names = generateNames(10000, 2014, 100);
cout << "names.size() = " << names.size() << '\n';
cout << '\n';
testMethod(names, "method 1 - string manipulation: ", rename_method_string);
cout << '\n';
testMethod(names, "method 2 - regular expressions: ", rename_method_regex);
return 0;
}
void testMethod(const vector<string> & names, const string & description, rename_method_t method)
{
vector<string> renamed(names.size());
clock_t timeStart = clock();
method(names, renamed);
clock_t timeEnd = clock();
cout << "renamed examples:\n";
for (int i = 0; i < 10 && i < names.size(); ++i)
cout << names[i] << " -> " << renamed[i] << '\n';
cout << description << 1000 * (timeEnd - timeStart) / CLOCKS_PER_SEC << " ms\n";
}
vector<string> generateNames(int lenPerYear, int yearStart, int years)
{
vector<string> result;
for (int year = yearStart, yearEnd = yearStart + years; year < yearEnd; ++year)
{
for (int i = 0; i < lenPerYear; ++i)
{
ostringstream oss;
oss << "Foo_" << i << "_Bar_" << year << ".jpg";
result.push_back(oss.str());
}
}
return result;
}
template<typename T>
bool equal_safe(T itShort, T itShortEnd, T itLong, T itLongEnd)
{
if (itLongEnd - itLong < itShortEnd - itShort)
return false;
return equal(itShort, itShortEnd, itLong);
}
void rename_method_string(const vector<string> & names, vector<string> & renamed)
{
//manually: "Foo_(\\d+)_Bar_(\\d+).jpg" -> \1_\2.jpg
const string foo = "Foo_", bar = "_Bar_", jpg = ".jpg";
for (int i = 0; i < names.size(); ++i)
{
const string & name = names[i];
//starts with foo?
if (!equal_safe(foo.begin(), foo.end(), name.begin(), name.end()))
{
renamed[i] = "ERROR no foo";
continue;
}
//extract number
auto it = name.begin() + foo.size();
for (; it != name.end() && isdigit(*it); ++it) {}
string str_num1(name.begin() + foo.size(), it);
//continues with bar?
if (!equal_safe(bar.begin(), bar.end(), it, name.end()))
{
renamed[i] = "ERROR no bar";
continue;
}
//extract number
it += bar.size();
auto itStart = it;
for (; it != name.end() && isdigit(*it); ++it) {}
string str_num2(itStart, it);
//check *.jpg
if (!equal_safe(jpg.begin(), jpg.end(), it, name.end()))
{
renamed[i] = "ERROR no .jpg";
continue;
}
renamed[i] = str_num1 + "_" + str_num2 + ".jpg";
}
}
void rename_method_regex(const vector<string> & names, vector<string> & renamed)
{
regex searching("Foo_(\\d+)_Bar_(\\d+).jpg");
smatch found;
for (int i = 0; i < names.size(); ++i)
{
if (regex_search(names[i], found, searching))
{
if (3 != found.size())
renamed[i] = "ERROR weird match";
else
renamed[i] = found[1].str() + "_" + found[2].str() + ".jpg";
}
else renamed[i] = "ERROR no match";
}
}
Run Code Online (Sandbox Code Playgroud)
它为我产生输出:
names.size() = 1000000
renamed examples:
Foo_0_Bar_2014.jpg -> 0_2014.jpg
Foo_1_Bar_2014.jpg -> 1_2014.jpg
Foo_2_Bar_2014.jpg -> 2_2014.jpg
Foo_3_Bar_2014.jpg -> 3_2014.jpg
Foo_4_Bar_2014.jpg -> 4_2014.jpg
Foo_5_Bar_2014.jpg -> 5_2014.jpg
Foo_6_Bar_2014.jpg -> 6_2014.jpg
Foo_7_Bar_2014.jpg -> 7_2014.jpg
Foo_8_Bar_2014.jpg -> 8_2014.jpg
Foo_9_Bar_2014.jpg -> 9_2014.jpg
method 1 - string manipulation: 421 ms
renamed examples:
Foo_0_Bar_2014.jpg -> 0_2014.jpg
Foo_1_Bar_2014.jpg -> 1_2014.jpg
Foo_2_Bar_2014.jpg -> 2_2014.jpg
Foo_3_Bar_2014.jpg -> 3_2014.jpg
Foo_4_Bar_2014.jpg -> 4_2014.jpg
Foo_5_Bar_2014.jpg -> 5_2014.jpg
Foo_6_Bar_2014.jpg -> 6_2014.jpg
Foo_7_Bar_2014.jpg -> 7_2014.jpg
Foo_8_Bar_2014.jpg -> 8_2014.jpg
Foo_9_Bar_2014.jpg -> 9_2014.jpg
method 2 - regular expressions: 796 ms
Run Code Online (Sandbox Code Playgroud)
另外,我认为这完全没有意义,因为在您的示例中,实际的 I/O(获取文件名、重命名文件)将比任何 CPU 字符串操作慢得多。所以回答你的问题: