2

我只想提取双引号内的那些单词。所以,如果内容是:

Would "you" like to have responses to your "questions" sent to you via email?

答案必须是

1- 你 2- 问题

4

3 回答 3

2
std::string str("test \"me too\" and \"I\" did it");
std::regex rgx("\"([^\"]*)\""); // will capture "me too"
std::regex_iterator current(str.begin(), str.end(), rgx);
std::regex_iterator end;
while (current != end)
    std::cout << *current++;
于 2013-02-23T13:15:04.020 回答
1

如果你真的想使用正则表达式,你可以这样做:

#include <regex>
#include <sstream>
#include <vector>
#include <iostream>

int main() {
    std::string str = R"d(Would "you" like to have responses to your "questions" sent to you via email?)d";
    std::regex rgx(R"(\"(\w+)\")");
    std::smatch match;
    std::string buffer;
    std::stringstream ss(str);
    std::vector<std::string> strings;
    //Split by whitespaces..
    while(ss >> buffer) 
        strings.push_back(buffer);
    for(auto& i : strings) {
        if(std::regex_match(i,match, rgx)) {
            std::ssub_match submatch = match[1];
            std::cout << submatch.str() << '\n';
        }
    }
}

我认为只有 MSVC 和 Clang 应该支持,否则你可以像这样使用 boost.regex

于 2013-02-23T10:45:20.743 回答
0

使用此答案split()中的函数,然后提取奇数项:

std::vector<std::string> itms = split("would \"you\" like \"questions\"?", '"');
for (std::vector<std::string>::iterator it = itms.begin() + 1; it != itms.end(); it += 2) {
    std::cout << *it << endl;
}
于 2013-02-23T10:38:20.517 回答