#include <regex>
// https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string
std::vector<std::string> tokenize(const std::string& str,
const std::string& delimiters = " ",
bool trimEmpty = false)
{
std::vector<std::string> tokens;
std::size_t start = 0, end, length = str.length();
while (length && start < length + 1)
{
end = str.find_first_of(delimiters, start);
if (end == std::string::npos)
{
end = length;
}
if (end != start || !trimEmpty)
tokens.push_back(std::string(str.data() + start, end - start));
start = end + 1;
}
return tokens;
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
fprintf(stderr, "not enough parameters\n");
return -1;
}
auto args = tokenize(argv[1], "/");
if (args.size() != 3)
{
fprintf(stderr, "bad argument: %s\n", argv[1]);
return -1;
}
fprintf(stderr, "Using regex: %s\n", args[1].c_str());
fprintf(stderr, "Using replace: %s\n", args[2].c_str());
std::regex re(args[1]);
while (!feof(stdin))
{
char line[1024];
if (fgets(line, 1024, stdin))
{
std::string s = regex_replace(line, re, args[2].c_str());
printf("%s\n", s.c_str());
}
}
return 0;
}