regex_search matches correctly but memory holder contains wrong value

Hi all, I have a small problem when trying out a regex that works ok with both jakarta (java code) and Perl. However, in boost::regex I get an extra space that's very weird. This is the code: #include <iostream> #include <string> #include <boost/regex.hpp> main() { static const boost::regex infileRedirection( "\\[(.+?)\\s*=\\s*(.+?)\\]" ); const string input = "[IN_FILE_REDIRECTION = STARTS_3]"; boost::cmatch what; if ( boost::regex_search( input, what, infileRedirection, boost::regbase::perl ) ) { cerr << "Matched! == input is (" << input << ")" << endl; string mySection; mySection.assign( what[1].first, what[1].second ); string newSectionName; newSectionName.assign( what[2].first, what[2].second ); cerr << "mySection is: --" << mySection << "--" << endl; cerr << "newSection is: --" << newSectionName << "--" << endl; } else { cerr << "Didn't match" << endl; } return 0; } And this is the output: [anibal@anibal ControlFile]$ ./a.out Matched! == input is ([IN_FILE_REDIRECTION = STARTS_3]) mySection is: --IN_FILE_REDIRECTION-- newSection is: -- STARTS_3-- Please note the extra space in the newSection. That should be STARTS_3 and not " STARTS_3". Why is it taking that space if the space itself is not in the () expression? Any help greatly appreciated, -Anibal

I have a small problem when trying out a regex that works ok with both jakarta (java code) and Perl. However, in boost::regex I get an extra space that's very weird. This is the code:
boost.regex follows the POSIX lefmost longest rules for matching expressions rather than the perl rules (yes you can have perl like syntax extentions, but you can't change the underlying state matchine). Most of the time this makes no difference, but sometimes it can bite you if you're used to perl's rules. In this case it's finding a match for $2 that's more to the left (and longer as it happens), than the match you were expecting. You might find an expression such as: "\\[(.+?)\\s*=\\s*([^\\s].*?)\\]" would do what you want. John Maddock http://ourworld.compuserve.com/homepages/john_maddock/index.htm
participants (2)
-
Anibal Jodorcovsky
-
John Maddock