Say you have a string which is provided by the user. It can contain any kind of character. Examples are:
std::string s1{"hello world"); std::string s1{".*"); std::string s1{"*{}97(}{.}}\\testing___just a --%#$%# literal%$#%^"}; ... Now I want to search in some text for occurrences of >> followed by the input string s1 followed by <<. For this, I have the following code:
std::string input; // the input text std::regex regex{">> " + s1 + " <<"}; if (std::regex_match(input, regex)) { // add logic here } This works fine if s1 did not contain any special characters. However, if s1 had some special characters, which are recognized by the regex engine, it doesn't work.
How can I escape s1 such that std::regex considers it as a literal, and therefore does not interpret s1? In other words, the regex should be:
std::regex regex{">> " + ESCAPE(s1) + " <<"}; Is there a function like ESCAPE() in std?
important I simplified my question. In my real case, the regex is much more complex. As I am only having troubles with the fact the s1 is interpreted, I left these details out.
string.finds1) as a regex?? Perhaps something likestd::regex Regex{">>*<<"};would be better?s1and some text. The strings1is used to dynamically construct the regex, which is used to search in the input text.