首先,我将很快描述我对此的动机和实际问题:
我不断处理大批量文件,更具体地说,我发现自己必须按照以下规则重命名:
它们可能都包含单词和数字,但只有一组数字递增而不是“常量”。我需要提取那些只有那些数字并相应地重命名文件。例如:
Foo_1_Bar_2015.jpg
Foo_2_Bar_2015.jpg
Foo_03_Bar_2015.jpg
Foo_4_Bar_2015.jpg
将重命名:
1.jpg
2.jpg
3.jpg or 03.jpg (The leading zero can stay or go)
4.jpg
所以我们开始的是一个带有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;
}
我之后所拥有的是(位置,大小)对的向量,用于文件名中的所有数字,常量(通过使用动机中的定义)或不。 在此之后,随之而来的是混乱,因为您需要交叉引用字符串并找出需要提取的数字。这将随着文件数量(往往是巨大的)而呈指数增长,而不是提到每个字符串中的数字序列数。此外,不是非常易读,可维护或优雅。不行。
2)正则表达式
如果有正则表达式的使用,那就是这个。从第一个文件名中创建一个正则表达式对象,并尝试将其与下一个文件名匹配。成功?即时提取所需数量的能力。失败?添加有问题的文件名作为新的正则表达式对象,并尝试匹配现有的两个正则表达式。冲洗并重复。正则表达式看起来像这样:
Foo_(\d+)_Bar_(\d+).jpg
或分别为每个数字序列创建一个正则表达式:
Foo_(\d+)_Bar_2015.jpg
Foo_1_Bar_(\d+).jpg
其余的是蛋糕。随便继续匹配,在最好的情况下,它可能只需要一次通过!问题是......
我需要知道的事情:
1)您能想到实现这一目标的任何其他优越方式吗?几天来,我一直在撞墙
2)尽管在第一种方法中字符串操作和矢量构造\破坏的成本可能很大,但与正则表达式对象的成本相比,它可能相形见绌。第二种方法,最坏的情况:与文件一样多的正则表达式对象。对于潜在的数千个文件,这会是灾难性的吗?
3)第二种方法可以针对以下两种可能性中的一种进行调整:很少std::regex
个对象构造,很多regex_match
个调用或者反过来。哪个更昂贵,正则表达式对象的构造或尝试匹配字符串?
答案 0 :(得分:2)
对我来说(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";
}
}
它为我产生输出:
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
此外,我认为这完全没有意义,因为在您的示例中,实际的I / O(获取文件名,重命名文件)将比任何CPU字符串操作慢得多。所以回答你的问题:
答案 1 :(得分:1)
为什么不使用split来分隔字母和数字之间的字符串:
Regex.Split(fileName, "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
然后获取数字所需的索引,可能使用Where子句,找到值增加的索引,而其他索引匹配,则可以使用.Last()获取扩展名。