Jump to content
  • Advertisement
Sign in to follow this  
SiS-Shadowman

using char* with boost::regex

This topic is 4418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
'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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!