Reifying regular expressions into std::regex objects
To use regular expressions in C++, you can't use a string such as "c[a-z]*t"
directly. Instead, you have to use that string to construct a regular expression object of type std::regex
, and then pass the regex
object as one of the arguments to a matching function such as std::regex_match
, std::regex_search
, or std::regex_replace
. Each object of type std::regex
encodes a complete finite state machine for the given expression, and constructing this finite state machine requires a lot of computation and memory allocation; so if we are going to match a lot of input text against the same regex, it is convenient that the library gives us a way to pay for that expensive construction just once. On the other hand, this means that the std::regex
objects are relatively slow to construct and expensive to copy; constructing a regex inside a tight inner loop is a good way to kill your program's performance:
std::regex rx("(left|right) ([0-9]+)");...