简体   繁体   English

Boost 正则表达式令牌迭代器:在括号之间获取输入

[英]Boost regex token iterator: getting input between parentheses

I'm using the following function with Boost::tr1::sregex_token_iterator我正在使用以下 function 和 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.如您所想, **s是指向字符串的指针,而 r 是有问题的正则表达式。 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).这个 function 工作(事实上,这个可能不起作用,因为我从原来的修改它只是为了使它更简单,因为 rest 与问题无关)。

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?我想知道的是,例如,给定这种正则表达式: "Email: (.*?) Phone:..." ,有没有办法只检索(.*?)部分,还是我应该在给定的结果上应用子字符串来实现这一点?

Else, it's going to throw out: Email: myemail@domain.com Phone: ..否则,它将丢弃:Email:myemail@domain.com 电话:..

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应该使用像Kerrek SB推荐的 regex_search: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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