Boost logo

Boost Users :

Subject: Re: [Boost-users] Pattern matching with boost
From: Igor R (boost.lists_at_[hidden])
Date: 2011-11-09 17:12:31


> I'm trying to parse a relatively simple pattern across 4 std::strings,
> extracting whatever the part which matches the pattern into a separate
> std::string.
>
> In an abstracted sense, here is what I want:
> s1=<string1><consecutive number>, s2=<consecutive number><string2>,
> s3=<string1><consecutive number>, s4=<consecutive number><string2>
>
> Less abstracted:
> s1="apple 1", s2="2 cheese", s3="apple 3", s4="4 cheese"
>
> Actual contents:
> s1="lxckvjlxcjvlkjlkje xvcjxzlvcj wqrej lxvcjz ljvl;x czvouzxvcu
> j;ljfds apple 1 xcvljxclvjx oueroi xcvzlkjv; zjx", s2="xzljlkxvc
> jlkjxzvl jxcvljzx lvjlkj wre 2 cheese", s3="apple 3", s4="kxclvj
> xcvjlxk jcvljxlck jxcvl 4 cheese"
>
> How would I perform this pattern matching?

Use Spirit.
I'm not sure I understand your grammar well, but try the following
trivial sample:

#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;
using qi::char_;
using qi::parse;

int main()
{
  std::string input = "abcd apple 1 qwerty", output;
  bool res = parse(input.begin(), input.end(), qi::omit[*(char_ -
(+char_("a-zA-Z") >> ' ' >> +char_("0-9")))] >> +char_("a-zA-Z") >>
qi::string(" ") >> +char_("0-9"), output);
}


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