# using char* with boost::regex

## Recommended Posts

is it possible to use char* with the boost::regex module? i haven't read anything about that in the manual so far, just that it's possible to use CString and other things with it. but i would appretiate it, if i could also use char* as an input for boost::regex_match. i tried just to give as an input a char* array, but i get errors, that the compiler couldn't derive the template-argument from char* for the first argument in the regex_match function.

##### Share on other sites
jflanglois    1020
It should work. It would probably be better if you first constructed an std::string from your char * so that going out of bounds will be less likely.

Using char * (not tested):
char *str = "Dude this is a string";
boost::regex_match< const char * >( str, str + 22, ... );

Better:
char *cstr = "Dude this is a string";
std::string str( cstr );
boost::regex_match< std::string::const_iterator >( str.begin(), str.end(), ... );

jfl.

##### Share on other sites
thanks for the response. i'll use the second method, but i've one further question. when i want to acces the result of the regex_match function, i just access the iterator at the specified index, this index should return a std::string, shouldn't it? unfortunately, this doesn't work:

boost::match_results<std::string::const_iterator> what;
printf("Var: %s %s", what[1].c_str(), what[2].c_str());

error C2039: 'c_str': Is no member of 'boost::sub_match<BidiIterator>' with
do i really need to create for each search result a std::string and then copy this one into a char*?

##### Share on other sites
Deyja    920
'what[#]' returns an std::pair<std::string::const_iterator,std::string::const_iterator> in this case. These are the start and end of the string.

Ripped right from my own code.
std::string group(boost::match_results<std::string::const_iterator>& what, int index){	return std::string(what[index].first,what[index].second);}

##### Share on other sites
ah, thanks very much :)