Skip to content

Instantly share code, notes, and snippets.

@drawcode
Created April 21, 2025 20:45
Show Gist options
  • Save drawcode/7b8fe1b55d1a0eec18fa06ad85d1e49f to your computer and use it in GitHub Desktop.
Save drawcode/7b8fe1b55d1a0eec18fa06ad85d1e49f to your computer and use it in GitHub Desktop.
RegexrReplace w/o C++20
#include <iostream>
#include <string>
#include <vector>
#include <regex>
#include <functional>
/// @brief Replaces occurrences of a regular expression pattern in a string with a replacement string.
///
/// @param input The string to search in.
/// @param pattern The regular expression pattern to match.
/// @param replacement The string to replace the matched occurrences with.
/// @return A new string with the replacements made.
std::string RegexReplace(const std::string& input, const std::string& pattern, const std::string& replacement) {
std::string result = "";
std::regex re(pattern);
std::smatch match;
std::string::const_iterator searchStart(input.cbegin());
while (std::regex_search(searchStart, input.cend(), match, re)) {
result.append(searchStart, match[0].first);
result.append(replacement);
searchStart = match[0].second;
}
result.append(searchStart, input.cend());
return result;
}
/// @brief Replaces occurrences of a regular expression pattern in a string with the result of a lambda function.
///
/// @tparam ReplacementFunc The type of the replacement function (must be callable with a std::smatch).
/// @param input The string to search in.
/// @param pattern The regular expression pattern to match.
/// @param replacementFunc A function (or lambda) that takes a std::smatch object and returns the replacement string.
/// @return A new string with the replacements made.
template<typename ReplacementFunc>
std::string RegexReplace(const std::string& input, const std::string& pattern,
ReplacementFunc replacementFunc) {
// Check if the ReplacementFunc is a string. If it is, call the string overload.
if constexpr (std::is_same_v<ReplacementFunc, std::string>) {
return RegexReplace(input, pattern, "");
}
std::string result = "";
std::regex re(pattern);
std::smatch match;
std::string::const_iterator searchStart(input.cbegin());
while (std::regex_search(searchStart, input.cend(), match, re)) {
result.append(searchStart, match[0].first);
result.append(replacementFunc(match)); // Call the lambda
searchStart = match[0].second;
}
result.append(searchStart, input.cend());
return result;
}
/// @brief Main function to run the Google Tests.
int main(int argc, char** argv) {
std::string text = "apple, banana, cherry";
std::string pattern = "(\\w+)";
std::string expected = "APPLE, BANANA, CHERRY";
std::string result = RegexReplace(text, pattern,
[](const std::smatch& m) -> std::string
{
std::string word = m[1].str();
std::string upperCaseWord = "";
for (char c : word)
{
upperCaseWord += std::toupper(c);
}
return upperCaseWord;
});
std::cout << "Original: " << text << std::endl;
std::cout << "Replaced: " << result << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment