Abaixo está o meu código a seguir
#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";
}
}
É sempre saídas sem correspondências.
Tenho certeza de que o regex deve funcionar.
Eu usei este exemplo
http://onlamp.com/pub/a/onlamp/2006/04/06/boostregex.html?page=3
Respostas:
3 para resposta № 1a partir de aumentar regex:
Importante
Note que o resultado só é verdadeiro se oa expressão corresponde à totalidade da sequência de entrada. Se você quiser procurar uma expressão em algum lugar dentro da sequência, use regex_search. Se você quiser corresponder um prefixo da sequência de caracteres, use regex_search com o sinalizador match_continuous set.
0 para resposta № 2
Experimentar boost::regex re("(.*)name(.*)");
se você quiser usar a expressão com regex_match
.