AlgoPlus v0.1.0
Loading...
Searching...
No Matches
find_and_replace.h
1#ifndef ALGOPLUS_REGEX_PATTERNS_H
2#define ALGOPLUS_REGEX_PATTERNS_H
3
4#ifdef __cplusplus
5#include <regex>
6#include <string>
7#endif
8
24void find_and_replace_regex(std::string& target, const std::string pattern,
25 const std::string newstr) {
26 std::regex regex_pattern(pattern);
27 target = std::regex_replace(target, regex_pattern, newstr);
28}
29
44void find_and_replace(std::string& str, const std::string pattern, const std::string newstr) {
45 std::string::size_type pos = 0u;
46 while ((pos = str.find(pattern, pos)) != std::string::npos) {
47 str.replace(pos, pattern.length(), newstr);
48 pos += newstr.length();
49 }
50}
51
52#endif // ALGOPLUS_REGEX_PATTERNS_H