fork download
  1. #include <regex>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. std::string regexEscape(std::string str) {
  7. return std::regex_replace(str, std::regex(R"([.^$|()[\]{}*+?\\])"), R"(\$&)");
  8. }
  9. int main()
  10. {
  11. std::cout << "Test escaped pattern: " << regexEscape("[da-d$\\]") << std::endl; // = > \[da-d\$\\\]
  12. std::string key = "\\56";
  13. string input = "John\\56 Fred\\12";
  14. std::regex rx(R"((\w+))" + regexEscape(key));
  15. smatch m;
  16. if (std::regex_search(input, m, rx)) {
  17. std::cout << "Who has \\56? - " << m[1].str() << std::endl;
  18. }
  19. }
Success #stdin #stdout 0s 3548KB
stdin
Standard input is empty
stdout
Test escaped pattern: \[da-d\$\\\]
Who has \56? - John