1. 程式人生 > >c++使用正則表示式提取關鍵字

c++使用正則表示式提取關鍵字

    string text = "岳雲鵬的物件叫鐵錘";
    regex  pattern("(.*)的物件叫(.*)");

    smatch results;
    if (regex_match(text, results, pattern)) {
    
        for (auto it = results.begin(); it != results.end(); ++it)
            cout <<  *it << endl;
    }
    else {
        cout << "match failed: " << text << endl;
    }
    // 岳雲鵬的物件叫鐵錘
    // 岳雲鵬
    // 鐵錘