33 lines
945 B
C++
33 lines
945 B
C++
|
|
#include "ml_string.h"
|
|
|
|
|
|
|
|
// Replace a substring with a replacement string in a source string
|
|
void replace_substring(std::string &src, const std::string &substr, const std::string &replacement) {
|
|
size_t i = 0;
|
|
for (i = src.find(substr, i); i != std::string::npos; i = src.find(substr, i)) {
|
|
src.replace(i, substr.size(), replacement);
|
|
i += replacement.size();
|
|
}
|
|
}
|
|
|
|
|
|
// Returns true if where contains regex
|
|
bool regexp_search(const std::string &where, const std::string ®ex_str) {
|
|
// online tester https://www.regextester.com/97722
|
|
std::regex regex(regex_str);
|
|
std::smatch match;
|
|
|
|
if(std::regex_search(where, match, regex)) {
|
|
// std::cout << "matches for '" << where << "'\n";
|
|
// std::cout << "Prefix: '" << match.prefix() << "'\n";
|
|
// for (size_t i = 0; i < match.size(); ++i)
|
|
// std::cout << i << ": " << match[i] << '\n';
|
|
// std::cout << "Suffix: '" << match.suffix() << "\'\n\n";
|
|
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|