以下は私の次のコードです
#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
回答:
回答№1の場合は3から 正規表現を後押しする:
重要
結果が真になるのは、expressionは、入力シーケンス全体に一致します。シーケンス内のどこかで式を検索する場合は、regex_searchを使用します。文字列のプレフィックスを一致させたい場合は、フラグmatch_continuousを設定してregex_searchを使用します。
回答№2の場合は0
試す boost::regex re("(.*)name(.*)");
式を使用したい場合 regex_match
.