0

以下は私の次のコードです

#include <iostream>
#include <stdlib.h>
#include <boost/regex.hpp>
#include <string>
using namespace std;
using namespace boost;

int main() {

    std::string s = "Hello my name is bob";
    boost::regex re("name");
    boost::cmatch matches;

    try{

        // if (boost::regex_match(s.begin(), s.end(), re))
        if (boost::regex_match(s.c_str(), matches, re)){

            cout << matches.size();

            // matches[0] contains the original string.  matches[n]
            // contains a sub_match object for each matching
            // subexpression
            for (int i = 1; i < matches.size(); i++){
                // sub_match::first and sub_match::second are iterators that
                // refer to the first and one past the last chars of the
                // matching subexpression
                string match(matches[i].first, matches[i].second);
                cout << "\tmatches[" << i << "] = " << match << endl;
            }
        }
        else{
            cout << "No Matches(" << matches.size() << ")\n";
        }
    }
    catch (boost::regex_error& e){
        cout << "Error: " << e.what() << "\n";
    }
}

常に一致なしで出力します。

正規表現が機能するはずです。

この例を使用しました

http://onlamp.com/pub/a/onlamp/2006/04/06/boostregex.html?page=3

4

2 に答える 2

3

ブースト正規表現から:

重要

式が入力シーケンス全体に一致する場合にのみ、結果が真になることに注意してください。シーケンス内のどこかで式を検索する場合は、regex_searchを使用します。文字列のプレフィックスを一致させる場合は、フラグmatch_continuousを設定してregex_searchを使用します。

于 2010-11-15T12:05:27.393 に答える
0

boost::regex re("(.*)name(.*)");式を。で使用する場合は試してくださいregex_match

于 2010-11-15T12:11:53.610 に答える