Compiletime build up of std::regex

A CppCon 2017 lightning talk by Hana Dusikova "Regular Expressions Redefined in C++” described an approach to compile-time regular expressions using a user-defined literal for regex strings and a compile-time approach to generating the matching function. The code is on GitHub, but is still experimental and highly fluid at this time. So it seems that compile-time regexes are probably going to appear sometime soon.


We need to distinguish between program compile and regex compile. The latter is really done at a program runtime and it means building a large but efficient structure (state machine) suitable for fast matching against various strings.

in c++11 regex, regex compilation is done when you construct a regex object of string:

std::regex e (your_re_string);

If you use such an object in regex_match, regex_search, regex_replace, you take the advantage of working with an already-compiled regular expression. So, if you know your string at program compile time, the best thing you can do for the sake of speed is to construct a corresponding regex object just once per program run, say, having it somewhere declared as a static variable with initializer:

static  std::regex e (your_constant_re_string);

Probably it is what you want.

Some forms of regex_match, ... function may work immediately with regular expression strings instead. But please note that although it's usually more convenient for a programmer, if you use them, the performance will suffer of doing regex compiling every time such a function called.

P.S. If you really, really, really want to have you regexp compiled at a program compile time, you may (1) Use an external regexp/lexer compiler software (like https://github.com/madelson/PrecompiledRegex.Fody, Flex https://en.wikipedia.org/wiki/Flex_(lexical_analyser_generator) or similar) (2) compile an std::regex object, then serialize and convert to C++ input (which is actually a DIY version of (1)) But I'm quite sure that it doesn't worth if only wanted in order to save one regex compile per program run. Maybe unless you have really overwhelming expressions.