正则表达式:C ++在双引号内提取文本

时间:2013-02-23 10:22:23

标签: c++

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

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

答案必须是

1-你 2-问题

3 个答案:

答案 0 :(得分:1)

如果您真的想使用Regex,可以这样做:

#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 like so

答案 1 :(得分:0)

使用this answer中的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;
}

答案 2 :(得分:0)

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++;