Boost logo

Boost :

From: Dave Jenkins (david_at_[hidden])
Date: 2007-11-23 10:47:36


"Jorge Lodos Vigil" <lodos_at_[hidden]> wrote in message
news:ECBF993526E3BC47BD232BDC77D6933706A916F4E9_at_mercurio.segurmatica.cu...
> Hi
> We are using xpressive with a grammar to match certain patterns. In some
> cases, we have the need to ignore white spaces.
> Using dynamic regexes, this can be achieved with the ignore_white_space
> constant.
> Is there a way to ignore white spaces using a grammar in xpressive other
> than modifying the grammar itself?

How about using a filter_iterator to skip spaces. Something like the
following program:

#include <iostream>
#include <string>
#include <boost/config.hpp>
#include <boost/iterator/filter_iterator.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/xpressive/xpressive_static.hpp>

struct not_space {
  inline bool operator()(char ch) const { return ' ' != ch; }
};

typedef boost::filter_iterator<not_space, std::string::const_iterator>
Iter_Skip;

boost::iterator_range<Iter_Skip> make_range (std::string& s)
{
    return boost::make_iterator_range(
        boost::make_filter_iterator<not_space>(s.begin(), s.end()),
        boost::make_filter_iterator<not_space>(s.end(), s.end())
        );
}

int main()
{
    using namespace boost::xpressive;

    std::string s = "aa bb cc";

    typedef basic_regex<Iter_Skip> regex_skip;
    regex_skip rx = as_xpr("aa") >> "bbcc";
    match_results<Iter_Skip> what;

    if(!regex_match(make_range(s), what, rx))
        std::cout << "not found\n";
    else
        std::cout << "found\n";
  return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk