string FileSystem::GetStringBetweenExpressions(const string& str, const string& firstExpression, const string& secondExpression) { // ("The quick brown fox", "The ", " brown") -> "quick" regex base_regex(firstExpression + "(.*)" + secondExpression); smatch base_match; if (regex_search(str, base_match, base_regex)) { // The first sub_match is the whole string; the next // sub_match is the first parenthesized expression. if (base_match.size() == 2) { return base_match[1].str(); } } return str; }
int main() { // Simple regular expression matching std::string fnames[] = {"foo.txt", "bar.txt", "baz.dat", "zoidberg"}; std::regex txt_regex("[a-z]+\\.txt"); for (const auto &fname : fnames) { std::cout << fname << ": " << std::regex_match(fname, txt_regex) << '\n'; } // Extraction of a sub-match std::regex base_regex("([a-z]+)\\.txt"); std::smatch base_match; for (const auto &fname : fnames) { if (std::regex_match(fname, base_match, base_regex)) { // The first sub_match is the whole string; the next // sub_match is the first parenthesized expression. if (base_match.size() == 2) { std::ssub_match base_sub_match = base_match[1]; std::string base = base_sub_match.str(); std::cout << fname << " has a base of " << base << '\n'; } } } // Extraction of several sub-matches std::regex pieces_regex("([a-z]+)\\.([a-z]+)"); std::smatch pieces_match; for (const auto &fname : fnames) { if (std::regex_match(fname, pieces_match, pieces_regex)) { std::cout << fname << '\n'; for (size_t i = 0; i < pieces_match.size(); ++i) { std::ssub_match sub_match = pieces_match[i]; std::string piece = sub_match.str(); std::cout << " submatch " << i << ": " << piece << '\n'; } } } }