如何加快正则表达式搜索C++中大量潜在的大文件?
How to speed up regex searching for large quantity of potentially large files in C++?
我正在尝试制作一个程序,使用 excel 文档作为配置文件来读取用户输入的通配符文件和通配符字符串。例如,用户可能能够在 C:\Read*.txt 中输入,并且 C 驱动器中以 Read 开头的任何文件,然后在读取后的任何字符和文本文件都将包含在搜索中。
他们可以搜索消息:*,所有以"消息:"开头并以任何字符序列结尾的字符串都将匹配。
到目前为止,它是一个工作程序,但问题是速度效率非常糟糕,我需要它能够搜索非常大的文件。我正在使用文件流和正则表达式类来执行此操作,但我不确定是什么花费了这么多时间。
我的代码中的大部分时间都花在下面的循环中(我只包含了 while 循环上方的行,以便您可以更好地理解我想要做什么):
smatch matches;
vector<regex> expressions;
for (int i = 0; i < regex_patterns.size(); i++){expressions.emplace_back(regex_patterns.at(i));}
auto startTimer = high_resolution_clock::now();
// Open file and begin reading
ifstream stream1(filePath);
if (stream1.is_open())
{
int count = 0;
while (getline(stream1, line))
{
// Continue to next step if line is empty, no point in searching it.
if (line.size() == 0)
{
// Continue to next step if line is empty, no point in searching it.
continue;
}
// Loop through each search string, if match, save line number and line text,
for (int i = 0; i < expressions.size(); i++)
{
size_t found = regex_search(line, matches, expressions.at(i));
if (found == 1)
{
lineNumb.push_back(count);
lineTextToSave.push_back(line);
}
}
count = count + 1;
}
}
auto stopTimer = high_resolution_clock::now();
auto duration2 = duration_cast<milliseconds>(stopTimer - startTimer);
cout << "Time to search file: " << duration2.count() << "n";
有没有比这更好的文件搜索方法?我尝试查找很多东西,但到目前为止还没有找到我理解的编程示例。
按优先级顺序排列的一些想法:
-
您可以将所有正则表达式模式连接在一起以形成单个正则表达式,而不是在每行上匹配
r
正则表达式。这将使您的程序速度提高r
倍。示例:(R1)|(R2)|(...)|(Rr)
-
确保在使用前编译正则表达式。
- 不要将最终
.*
添加到正则表达式模式中。
一些想法,但不可移植:
- 内存映射文件而不是读取 iostreams
- 考虑是否值得重新实现
grep
而不是通过popen()
调用grep
相关文章:
- 使用 boost::regex 从目录中获取带有一些正则表达式的文件名称时出现意外输出
- 使用正则表达式从文件中删除行注释
- 如何加快正则表达式搜索C++中大量潜在的大文件?
- 需要正则表达式来提取zip文件中的所有文件
- 使用正则表达式从文件中读取数据信息
- 运行正则表达式时未选中的异常 - 从文件路径获取不带扩展名的文件名
- 有效文件夹名称C (Windows)的正则表达式
- 在Qt Creator中使用正则表达式在多个文件中查找和替换
- 使用c++正则表达式验证ASCII GnuPlot文件
- Boost.Test 使用正则表达式测试文件内容
- 在C++中使用正则表达式搜索.txt文件
- 通过从文件读取来增强正则表达式匹配
- 如何使用c++ 11正则表达式拆分css文件上的令牌
- 从文件中获取行并用作正则表达式(regex)
- 正则表达式替换文件扩展名前缀放置不起作用 - 替换匹配项
- 使用boost正则表达式解析文本文件
- 用C++正则表达式搜索字符串中的文件路径
- 使用C++11正则表达式捕获上下文无关语法文件的内容
- ATL正则表达式解析csv文件
- 从配置文件中动态加载正则表达式模式