jeudi 25 mai 2017

Need to get substring between two substrings in c++, the second substring should be first occurence after the first substring

I am trying to get substring between sStr1 and sStr2, the condition is i want sStr2 should be first occurence after sStr1, like if i sStr1 =", " and sStr2 = " " then rStr should be "2017", but currently i am getting as "2017 at 01:05:00". What changes should i do in this code?

void getBtwString(std::string oStr, std::string sStr1, std::string sStr2, std::string &rStr)
{
    using namespace std::literals::string_literals;
    auto start = sStr1;
    auto end = sStr2;
    std::regex base_regex(start + "(.*)" + end);
    auto example = oStr;
    std::smatch base_match;
    std::string matched;
    if (std::regex_search(example, base_match, base_regex)) 
    {
        if (base_match.size() == 2) 
        {
            matched = base_match[1].str();
        }
        rStr = matched;
    }
}

int main(void) 
{
    string d1 = "May 23, 2017 at 01:05:00 PM";
    string d2 = "June 24, 2017 at 01:05:00 PM";

    string strout;

    getBtwString(d1, ", ", " ", strout); // second solution
    cout<< strout;
    return 1;
}

Aucun commentaire:

Enregistrer un commentaire