fork(1) download
  1. #include <regex>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. std::string remove_password(std::string const& input)
  6. {
  7. // I think this should work for skipping escaped quotes in the password.
  8. // It works in javascript, but not in the standard library implementation.
  9. // anyone have any ideas?
  10. // (.*password\(("|'))(?:\\\2|[^\2])*?(\2.*)
  11. // const char prog[] = R"__regex((.*password\(')([^']*)('.*)))__regex";
  12. const char prog[] = R"((.*password\((["']))(?:\\\2|(?!\2)[^])*?(\2.*))";
  13. auto reg = std::regex(prog, std::regex_constants::ECMAScript);
  14. std::smatch match;
  15. std::regex_match(input, match, reg);
  16. // match[0] is the entire string
  17. // match[1] is pre-password
  18. // match[2] is the password
  19. // match[3] is post-password
  20. return match[1].str() + "********" + match[3].str();
  21. }
  22.  
  23. int main()
  24. {
  25. using namespace std::literals;
  26.  
  27. auto test_string = R"__(select * from run_on_hive(server('hdp230m2.labs.teradata.com'),username('vijay'),password('vijay'),dbname('default'),query('analyze table default.test01 compute statistics'));)__";
  28.  
  29. std::cout << remove_password(test_string);
  30. }
Success #stdin #stdout 0s 4176KB
stdin
Standard input is empty
stdout
select * from run_on_hive(server('hdp230m2.labs.teradata.com'),username('vijay'),password('********'),dbname('default'),query('analyze table default.test01 compute statistics'));