The following code is generalized. Supports PCRE , PCRE2 and stl regex libs
bool U::String::replaceExAll(string &s, const string& replace_this_reg_ex, const string& replace_with, bool case_sensitive, bool extended) { #ifdef UTIL_USE_PCRE pcrecpp::RE_Options options; options.set_utf8(true); options.set_caseless(!case_sensitive); pcrecpp::RE(replace_this_reg_ex, options).GlobalReplace(replace_with, &s); return true; #elif UTIL_USE_PCRE2 jp8::Regex re(replace_this_reg_ex); jp8::RegexReplace& rp = re.initReplace(); rp.setSubject(s) .setReplaceWith(replace_with) .setBufferSize(s.length() * 2); if(!case_sensitive) rp.addPcre2Option(PCRE2_CASELESS); if(extended) rp.addPcre2Option(PCRE2_SUBSTITUTE_EXTENDED); rp.addPcre2Option(PCRE2_SUBSTITUTE_GLOBAL);
source share