Created
April 21, 2025 20:45
-
-
Save drawcode/7b8fe1b55d1a0eec18fa06ad85d1e49f to your computer and use it in GitHub Desktop.
RegexrReplace w/o C++20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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