Created
April 21, 2015 04:46
-
-
Save 1995eaton/2a8741eaad83b09797f9 to your computer and use it in GitHub Desktop.
Regex Utils for C++11
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 <algorithm> | |
#include <vector> | |
#include <regex> | |
std::regex::flag_type parse_flags(const std::string& flag_str) { | |
std::regex::flag_type grammar = std::regex::ECMAScript; | |
std::regex::flag_type flags = grammar ^ grammar; | |
for (const auto& c: flag_str) { | |
switch (c) { | |
case 'i': flags |= std::regex::icase; break; | |
case 'o': flags |= std::regex::optimize; break; | |
case 'c': flags |= std::regex::collate; break; | |
case 'n': flags |= std::regex::nosubs; break; | |
case 'e': flags |= std::regex::ECMAScript; break; | |
case 'x': flags |= std::regex::extended; break; | |
case 'a': flags |= std::regex::awk; break; | |
case 'b': flags |= std::regex::basic; break; | |
case 'G': flags |= std::regex::grep; break; | |
case 'E': flags |= std::regex::egrep; break; | |
} | |
} | |
return flags | grammar; | |
} | |
const std::vector<std::string> regex_match( | |
const std::string& str, | |
const std::string& _rxp, | |
const std::string& flags = "") { | |
std::vector<std::string> result; | |
std::regex rxp(_rxp, parse_flags(flags)); | |
std::transform(std::sregex_iterator(str.begin(), str.end(), rxp), | |
std::sregex_iterator(), | |
std::back_inserter(result), | |
[](const auto& e) { | |
return e.str(); | |
}); | |
return result; | |
} | |
bool regex_test(const std::string& str, | |
const std::string& _rxp, | |
const std::string& flags = "") { | |
std::regex rxp(_rxp, parse_flags(flags)); | |
return std::regex_search(str, rxp); | |
} | |
const std::vector<std::string> regex_split( | |
const std::string& str, | |
const std::string& _rxp, | |
const std::string& flags = "") { | |
std::vector<std::string> result; | |
std::regex rxp(_rxp, parse_flags(flags)); | |
size_t pos = 0; | |
bool match_found = false; | |
std::for_each(std::sregex_iterator(str.begin(), str.end(), rxp), | |
std::sregex_iterator(), | |
[&](const auto& e) { | |
result.push_back(str.substr(pos, e.position() - pos)); | |
pos = e.position() + e.length(); | |
match_found = true; | |
}); | |
if (!match_found) | |
result.push_back(str); | |
return result; | |
} | |
std::string regex_sub( | |
const std::string& str, | |
const std::string& _rxp, | |
const std::string& rep, | |
const std::string& flags = "") { | |
std::regex rxp(_rxp, parse_flags(flags)); | |
return std::regex_replace(str, rxp, rep); | |
} | |
std::string regex_sub( | |
const std::string& str, | |
const std::string& _rxp, | |
std::function<std::string(const std::string&)> rep, | |
const std::string& flags = "") { | |
std::string result; | |
std::regex rxp(_rxp, parse_flags(flags)); | |
size_t pos = 0; | |
bool match_found = false; | |
std::for_each(std::sregex_iterator(str.begin(), str.end(), rxp), | |
std::sregex_iterator(), | |
[&](const auto& e) { | |
result += str.substr(pos, e.position() - pos); | |
result += rep(e.str()); | |
pos = e.position() + e.length(); | |
match_found = true; | |
}); | |
if (!match_found) | |
result = str; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment