Boost logo

Boost Users :

From: John Maddock (john_maddock_at_[hidden])
Date: 2002-07-26 08:01:03


> Any help is certainly appreciated. Thanks.

Remember that if the expression matches a non-quoted string then the bit you
want is actually in $2 not $1, here's what I came up with:

   boost::regex exp(
      "\"((?:[^\\\\\"]|\\\\.)+)\"[:space:]*"
      "|"
      "([^[:space:]]+)[:space:]*"
   );
   std::string str = "parm1 \"parm2 parm3\" \"parm4 \\\"yo\\\"\"";
   std::string::const_iterator start = str.begin(), end = str.end();
   boost::match_results<std::string::const_iterator> what;
   unsigned int flags = boost::match_default;

   while (boost::regex_search(start, end, what, exp, flags)) {
      std::string spec(what[0]);
      std::string match;
      if(what[1].matched)
         match = what[1]; // quoted string
      else
         match = what[2]; // non-quoted string
      start = what[0].second;
      std::cout << spec << std::endl;
      std::cout << match << std::endl << std::endl;
   }

which prints:

parm1
parm1

"parm2 parm3"
parm2 parm3

"parm4 \"yo\""
parm4 \"yo\"

which looks like what you want?

John Maddock
http://ourworld.compuserve.com/homepages/john_maddock/index.htm


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net