[英]Reducing std::regex compile time in C++
我正在使用std::regex r("-?[0-9]*(.[0-9]+)?(e-?[0-9]+)?")
来验证数字(整数/固定点) /浮点)。 MWE如下:
#include <iostream>
#include <string>
#include <vector>
#include <regex>
#include <algorithm>
using namespace std;
bool isNumber(string s) {
// can ignore all whitespace
s.erase(remove(s.begin(), s.end(), ' '), s.end());
std::regex r("-?[0-9]*(.[0-9]+)?(e-?[0-9]+)?");
return regex_match(s, r);
}
int main() {
std::vector<string> test{
"3", "-3", // ints
"1 3", " 13", " 1 3 ", // weird spacing
"0.1", "-100.123123", "1000.12312", // fixed point
"1e5", "1e-5", "1.5e-10", "1a.512e4", // floating point
"a", "1a", "baaa", "1a", // invalid
"1e--5", "1e-", //invalid
};
for (auto t : test) {
cout.width(20); cout << t << " " << isNumber(t) << endl;
}
return 0;
}
我注意到与我期望的相比,编译时间非常大:
gcc 5.4 -O0 -std=c++11
秒 gcc 5.4 -O2 -std=c++11
秒 clang++ 3.8 -O0 -std=c++11
秒 clang++ 3.8 -O2 -std=c++11
秒 我将此用于在线评审提交,在编译阶段有时间限制。
所以,一个重要的问题:
你可以通过适当地转义小数点来减轻你的正则表达式的计算负荷: -?[0-9]*(\\.[0-9]+)?(e-?[0-9]+)?
这当然会阻止你对诸如“1 3”之类的数字返回误报(不要担心那些是2个数字是好事。)但在这种情况下以及许多其他当你弯腰使用正则表达式时“现在你有两个问题“ 。
使用istringstream
将为此问题提供更专业,更强大的解决方案:
bool isNumber(const string& s) {
long double t;
istringstream r(s);
return r >> t >> ws && r.eof();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.