I have the following function, written in C++11, that uses regular expressions from the standard regex module. As its name suggests, it determines whether the given string str is a number prefixed with the given prefix.
bool isPrefixedNumber(const std::string &prefix, const std::string &str) { std::regex re(prefix + "[[:digit:]]+")) return std::regex_match(str, re); } The problem is that I want prefix to be taken literally, i.e. I want
isPrefixedNumber("t.st", "tXst123") to return false. Is there a way of constructing such a regular expression without the need to manually escape all occurrences of special characters in prefix? In another words, how to prevent the interpretation of special characters when passing a std::string into std::regex?
Note: The above function is just a simple illustration of my question. I need to prevent interpretation of strings in a much larger regular expression. I do not need another way of re-writing the function without regular expressions.