string regex function added
This commit is contained in:
@@ -49,6 +49,55 @@ std::vector<std::string> regexp_strsplit(const std::string &string_to_split, con
|
||||
return elems;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> regexp_match(const std::string &str, const std::string ®_ex, bool case_sensitive) {
|
||||
std::vector<std::string> matches;
|
||||
|
||||
std::regex pars_regex(reg_ex); // , (case_sensitive ? std::regex::basic : std::regex::basic|std::regex::icase));
|
||||
auto words_begin = std::sregex_iterator(str.begin(), str.end(), pars_regex);
|
||||
auto words_end = std::sregex_iterator();
|
||||
|
||||
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
|
||||
std::smatch match = *i;
|
||||
matches.push_back(match.str());
|
||||
}
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
std::vector<std::vector<std::string>> regexp_tokens(const std::string &str, const std::string ®_ex, bool case_sensitive) {
|
||||
std::vector<std::vector<std::string>> captured_groups;
|
||||
std::vector<std::string> captured_subgroups;
|
||||
|
||||
std::smatch res;
|
||||
std::regex exp(reg_ex); // , (case_sensitive ? std::regex::basic : std::regex::basic|std::regex::icase));
|
||||
std::string::const_iterator searchStart(str.cbegin());
|
||||
while (std::regex_search(searchStart, str.cend(), res, exp)) {
|
||||
captured_subgroups.clear();
|
||||
if (res.size() == 1) { // no subgroups
|
||||
captured_subgroups.push_back(res[0]);
|
||||
} else {
|
||||
for (size_t i = 1; i < res.size(); ++i) { // [0] is whole match
|
||||
captured_subgroups.push_back(res[i]);
|
||||
}
|
||||
}
|
||||
if (captured_subgroups.size() > 0)
|
||||
captured_groups.push_back(captured_subgroups);
|
||||
|
||||
searchStart += res.position() + res.length();
|
||||
}
|
||||
return captured_groups;
|
||||
}
|
||||
|
||||
std::vector<std::vector<std::string>> regexp_search2(const std::string &string_to_split, const std::string &rgx_str, bool match_mode, bool ignore_case) {
|
||||
if (match_mode) {
|
||||
std::vector<std::vector<std::string>> matches{regexp_match(string_to_split, rgx_str, ignore_case)};
|
||||
return matches;
|
||||
} else {
|
||||
return regexp_tokens(string_to_split, rgx_str, ignore_case);
|
||||
}
|
||||
}
|
||||
|
||||
std::string string_lucase(std::string s, const std::string &strcase) {
|
||||
if (strcase == "upper")
|
||||
std::transform(s.begin(), s.end(),s.begin(), ::toupper);
|
||||
|
||||
Reference in New Issue
Block a user