简体   繁体   中英

Boost regex token iterator: getting input between parentheses

I'm using the following function with Boost::tr1::sregex_token_iterator

int regexMultiple(std::string **s, std::string r)
{
    std::tr1::regex term=(std::tr1::regex)r;
    const std::tr1::sregex_token_iterator end;
    int nCountOcurrences;

    std::string sTemp=**s;

    for (std::tr1::sregex_token_iterator i(sTemp.begin(),sTemp.end(), term); i != end; ++i)
    {
        (*s)[nCountOcurrences]=*i;
        nCountOcurrences++;
    }
    return nCountOcurrences;
}

As you can suppose, **s is a pointer to a string, and r is the regex in question. This function works (in fact, this one might not work because I modified it from the original just to make it simpler, given that the rest is not relevant to the question).

What I want to know is, given, for example, a regex of this kind: "Email: (.*?) Phone:..." , is there any way to retrieve only the (.*?) part from it, or should I apply substrings over the given result to achieve this instead?

Else, it's going to throw out: Email: myemail@domain.com Phone: ..

Thanks.

Should use regex_search like Kerrek SB recommended instead: http://www.boost.org/doc/libs/1_39_0/libs/regex/doc/html/boost_regex/ref/regex_search.html

int regexMultiple(std::string **s, std::string r)
{
    std::tr1::regex term=(std::tr1::regex)r;
    std::string::const_iterator start, end;
    boost::match_results<std::string::const_iterator> what;
    int nCountOcurrences=0;

    std::string sTemp=**s;
    start=sTemp.begin();
    end=sTemp.end();
    boost::match_flag_type flags = boost::match_default; 

    while (regex_search(start,end, what, term, flags))
    {
        (*s)[nCountOcurrences]=what[1];
        nCountOcurrences++;
        start = what[0].second;
        flags |= boost::match_prev_avail;
        flags |= boost::match_not_bob;
    }

    return nCountOcurrences;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM